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.