I'm learning C++ coming from a Java background (knowing a little C from many years ago)...
In Java, it's common practice to use "this" inside a constructor to distinguish the variable passed in as arguments to the constructor from the one declared in the class:
class Blabla {
private int a;
private int b;
Blabla(int a, int b){
this.a = a;
this.b = b;
}
}
I like this, because the variable Blabla.a and the one passed in as an argument to the constructor represents the same thing, so it feels logical that they should have the same name...
Is it possible to 开发者_运维问答do this in C++?
Yes, you can use this
to refer to member variables. That said, you'll often find that your code looks as follows in idiomatic C++:
class Blabla {
private:
int a_;
int b_;
public:
Blabla(int a, int b) : a_(a), b_(b) {}
};
As you can see, you normally do not apply the access control specifiers (public
, protected
or private
) to each member, but you group them in sections.
Also, if you use the type of initialisation that you used above, the members get initialised twice - once with the default constructor when the object is created (basically, before the code inside the curly brackets is executed) and the second time during the assignments to this->a
.
Yes, you use this->a = a;
Also note that in C++ you should use initialization lists in your constructors.
class Blab
{
public:
Blab(int a, int b)
:m_a(a),
m_b(b)
{
}
private:
int m_a;
int m_b;
};
Edit:
You can use :a(a), b(b)
in the initialization list using the same name for both your data member a and the parameter a passed into the constructor. They do not have to be different, but some feel that it is better practice to use a different naming convention for member variable names. I used different names in the example above to help clarify what the initialization list was actually doing and how it is used. You may use this->a
to set another member variable if you wish. For example if both the member variables and the parameter variables are both a and b,
class MyClass
{
public:
MyClass(int a, int b);
private:
int a;
int b;
};
// Some valid init lists for the MyClass Constructor would be
:a(a), b(b) // Init member a with param a and member b with parameter b
:a(a), b(this->a) // Init member a with param a and init member b with member a (ignores param b)
:a(5), b(25) // Init member a with 5 and init member b with 25 (ignoring both params)
It should be mentioned that initialization lists should init the member variables in the same order they appear in the class definition. A good compiler will give you warnings when you don't.
Since in C++ this
is a pointer, you access its member variables using this->var
.
Do note that C++ tends to have different conventions than Java, and it's common to use _var
for private variables, instead of the usual Java way of this.var
. Of course, it depends on the convention you want to use.
Just to summarize and put it into one answer.
If you would like to use the this->a method it would look like this
class Blabla
{
private:
int a;
int b;
public:
Blabla(int a, int b);
}
Blabla::Blabla(int a, int b)
{
this->a = a;
this->b = b;
}
Or if you use the C++ way with :a(a) it would look like this
class Blabla
{
private:
int a;
int b;
public:
Blabla(int a, int b);
}
Blabla::Blabla(int a, int b):a(a), b(b)
{
}
And personally I think that the this->a leads to more readable code than the hungarian notation examples that put letters in front like m_a or the more c style _a.
精彩评论