C++ Classes

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Regicide

Thread Starter
Joined
Aug 8, 2003
Messages
307
I'm working with one class in a program. In that class I have one function. For one of the arguments I want it to store the value to a protected member of the class:
Code:
class Test
{
public:
      int Function(char* X)
      {
            cout <<  X << endl;
            return 1;
       }
protected:
      char X[50];
};
When I do this, the protected X doesn't equal the X argumented in the function. Does anyone know how to do this without making the X in the function have a different name? Thanks.
 
Joined
Oct 7, 2003
Messages
1,741
i think u would find everything about java classes in a java class. try aptech, or informatics. no, really! ;)
 

Guy

Joined
Feb 19, 1999
Messages
260
Originally posted by Möbiús:
i think u would find everything about java classes in a java class. try aptech, or informatics. no, really! ;)
What are you talking about? He wants c++ help not java propeganda

On with the problem: to access a member variable withing a class, use the this operator:
Code:
class Test
{
public:
      int Function(char* X)
      {
            cout <<  this->X << endl; // or this.X I cant remember if this is a pointer or a value
            return 1;
       }
protected:
      char X[50];
};
 
Joined
Oct 9, 2001
Messages
27,087
Mobius, if you like Java, that's fine. Quit trying to get everybody to use Java, they use, what they use and are asking legitimate questions.

When you get to the real world, you'll realize, often you don't have a choice what language you use. It's allready been decided, you have to make it work.

So instead of getting people to change to JAVA, try helping them in the language they are asking for help in.

ps I think Guy got it, in OOP you have to be very careful about variable scope and what you are really accessing.
 
Joined
Oct 12, 2003
Messages
3,015
The X in the function declaration and the X in the class definition are different variables occupying different areas of memory. If you want to assign one to the other then do so; otherwise the protected member of your class is not going to be updated.

Also, I strongly encourage you to use different variable names in your class definition and function argument list. I hop that the code that you posted was an example and you don't in reality intend to use the same variable name twice in the same scope. This process only serves to confuse others trying to read your code.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Top