views:

166

answers:

9

I have a class defined like this:

class MyClass 
{
    int x;
    public: 
        MyClass(int x); 
};

MyClass::MyClass(int x)
{ //Assign x here 
}

However, I can't initialize x in the constructor because it has the same name as an instance variable. Is there any way around this(other than changing the name of the argument)?

A: 

Use this->x instead.

Ignacio Vazquez-Abrams
this is a pointer :)
Nikolai N Fetissov
`this` is a pointer in C++.
Chris Lutz
this is a pointer, use this->x and not this.x ;)
machielo
Well fine then :P
Ignacio Vazquez-Abrams
+2  A: 

this->x = x;

Nikolai N Fetissov
Seriously. You could not advice him to use a better name. And for more complex types that are not int this is the wrong answer anyway.
Martin York
Yeh, you are right. Better names, no extra initialization, etc. Thanks for pointing this out.
Nikolai N Fetissov
+11  A: 

The best option is to use the constructor's initializer list:

MyClass::MyClass(int x) : x( x ) { // Body }

But you could also try this approach:

MyClass::MyClass(int x) { this->x = x; }
Naaff
Thanks for explaining how to do this the "right" way!
Mike
@dirkgently GCC 4.2 doesn't complain about this... any reason that shadowing is bad?
Mike
@dirkgently -- shadowing might not be ideal, but it's valid C++. We could debate shadowing all day long, but what he wanted to know was C++ semantics.
Naaff
Seriously. You could not advice him to use a better name
Martin York
+2  A: 

You can use this to explicitly refer to the current object:

this->x = x;
sth
Seriously. You could not advice him to use a better name. And for more complex types that are not int this is the wrong answer anyway.
Martin York
How come that people with higher rep are laconically suggesting `this->x = x;` while people with lower rep are suggesting to rename the parameter? Superior technical knowledge vs common sense? :)
UncleBens
A: 

Use the this pointer

MyClass::MyClass(int x)
{
    this->x = x;
}

Of course not having colliding names like that in the first place would be a better solution.

Ruddy
I guess this combines the worst of both worlds. There is no need for `this->x = x`, which is poor form. Also, as *Scott Meyers* states in his **Effective C++** series, "Prefer initialization lists in constructors."
Thomas Matthews
A: 

this->x = x isn't working? That's what we did (or used a different parameter name).

LH
+3  A: 

as an aside - you really should have a naming convention for your member variables that does not clash. This is usually coding rules 1 or 2 for c++ houses. Then when you see m_foo = bar you know exactly what is going on

we use

 int m_thingy;

I have also seen

 int _thingy;
 int thingy_

apologies in advance if you knew this and could not or would not do it

pm100
Best to avoid identifiers that start with "_".
Bill
Ditto to Bill: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797
Martin York
+9  A: 

However, I can't initialize x in the constructor because it has the same name as an instance variable. Is there any way around this(other than changing the name of the argument)?

So change the name of the parameter!

class MyClass  
{ 
    int x; 
    public:  
        MyClass(int xInitVal);  
}; 

MyClass::MyClass(int xInitVal)
    :x(xInitVal)
{ // Don't assign x here.  
} 

By makeing the parameter name the same as a local you are just making the code hard to read. Don't do it. Nearly every style guide you come across will tell you not to make parameters the same name as members. A small bit of common sense please.

<rant> To all the people that answered:

 this->x = x; 

Don't ask me for a job. My god are you delibrately trying to cause problems.
The fact that it looks horrible is not a give away that this is a bad idea.

Yes it is techncially allowed but the whole point is to make code easy to read and maintain not try and make it an exotic art of decoding the intentions of the previous author.

</rant>

Martin York
+1  A: 

I strongly recommend you just change the variable names. Messing with duplicate identifiers is a fight without a cause.

In my code, I give all function parameters the prefix 'in' ("inValue"). I give all private member variables the prefix 'm' ("mValue").

TheBuzzSaw