How can I initialize C++ object member variables in the constructor?
You can specify how to initialize members in the member initializer list:
BigMommaClass {
BigMommaClass(int, int);
private:
ThingOne thingOne;
ThingTwo thingTwo;
};
BigMommaClass::BigMommaClass(int numba1, int numba2)
: thingOne(numba1 + numba2), thingTwo(numba1, numba2) {}
This question is a bit old, but here's another way in C++11 of "doing more work" in the constructor before initialising your member variables:
BigMommaClass::BigMommaClass(int numba1, int numba2)
: thingOne([](int n1, int n2){return n1+n2;}(numba1,numba2)),
thingTwo(numba1, numba2) {}
The lambda function above will be invoked and the result passed to thingOnes constructor. You can of course make the lambda as complex as you like.
You're trying to create a ThingOne
by using operator=
which isn't going to work (incorrect syntax). Also, you're using a class name as a variable name, that is, ThingOne* ThingOne
. Firstly, let's fix the variable names:
private:
ThingOne* t1;
ThingTwo* t2;
Since these are pointers, they must point to something. If the object hasn't been constructed yet, you'll need to do so explicitly with new in your BigMommaClass
constructor:
BigMommaClass::BigMommaClass(int n1, int n2)
{
t1 = new ThingOne(100);
t2 = new ThingTwo(n1, n2);
}
Generally initializer lists are preferred for construction however, so it will look like:
BigMommaClass::BigMommaClass(int n1, int n2)
: t1(new ThingOne(100)), t2(new ThingTwo(n1, n2))
{ }
I know this is 5 years later, but the replies above don't address what was wrong with your software. (Well, Yuushi's does, but I didn't realise until I had typed this - doh!). They answer the question in the title How can I initialize C++ object member variables in the constructor? This is about the other questions: Am I using the right approach but the wrong syntax? Or should I be coming at this from a different direction?
Programming style is largely a matter of opinion, but an alternative view to doing as much as possible in a constructor is to keep constructors down to a bare minimum, often having a separate initialization function. There is no need to try to cram all initialization into a constructor, never mind trying to force things at times into the constructors initialization list.
So, to the point, what was wrong with your software?
private:
ThingOne* ThingOne;
ThingTwo* ThingTwo;
Note that after these lines, ThingOne
(and ThingTwo
) now have two meanings, depending on context.
Outside of BigMommaClass, ThingOne
is the class you created with #include "ThingOne.h"
Inside BigMommaClass, ThingOne
is a pointer.
That is assuming the compiler can even make sense of the lines and doesn't get stuck in a loop thinking that ThingOne
is a pointer to something which is itself a pointer to something which is a pointer to ...
Later, when you write
this->ThingOne = ThingOne(100);
this->ThingTwo = ThingTwo(numba1, numba2);
bear in mind that inside of BigMommaClass
your ThingOne
is a pointer.
If you change the declarations of the pointers to include a prefix (p)
private:
ThingOne* pThingOne;
ThingTwo* pThingTwo;
Then ThingOne
will always refer to the class and pThingOne
to the pointer.
It is then possible to rewrite
this->ThingOne = ThingOne(100);
this->ThingTwo = ThingTwo(numba1, numba2);
as
pThingOne = new ThingOne(100);
pThingTwo = new ThingTwo(numba1, numba2);
which corrects two problems: the double meaning problem, and the missing new
. (You can leave this->
if you like!)
With that in place, I can add the following lines to a C++ program of mine and it compiles nicely.
class ThingOne{public:ThingOne(int n){};};
class ThingTwo{public:ThingTwo(int x, int y){};};
class BigMommaClass {
public:
BigMommaClass(int numba1, int numba2);
private:
ThingOne* pThingOne;
ThingTwo* pThingTwo;
};
BigMommaClass::BigMommaClass(int numba1, int numba2)
{
pThingOne = new ThingOne(numba1 + numba2);
pThingTwo = new ThingTwo(numba1, numba2);
};
When you wrote
this->ThingOne = ThingOne(100);
this->ThingTwo = ThingTwo(numba1, numba2);
the use of this->
tells the compiler that the left hand side ThingOne
is intended to mean the pointer. However we are inside BigMommaClass
at the time and it's not necessary.
The problem is with the right hand side of the equals where ThingOne
is intended to mean the class. So another way to rectify your problems would have been to write
this->ThingOne = new ::ThingOne(100);
this->ThingTwo = new ::ThingTwo(numba1, numba2);
or simply
ThingOne = new ::ThingOne(100);
ThingTwo = new ::ThingTwo(numba1, numba2);
using ::
to change the compiler's interpretation of the identifier.