How to Initialize C++ Object Member Variables in the Constructor

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) {}

How to create class member objects initialized during the constructor

The problem is that what you're calling "initialisation" is actually no such thing. Any members that are initialised, implicitly or explicitly, are initialised before the program enters your constructor body.

Your code snippets show only assignment; it doesn't matter that you're doing this in the body of a constructor for the encapsulating object. It's still just assignment.

Bar is a class so your member _bar will be implicitly initialised, but it actually cannot be because the class has no constructor taking no arguments. In order to provide arguments, you have to explicitly initialise the member yourself.

In C++ we initialise members like this:

class Foo {
public:
Foo(int valueBarNeeds)
: _bar(valueBarNeeds)
{}
private:
Bar _bar;
};

You are also right in that you are misunderstanding new a little; unlike in Java, it should be used sparingly, as objects are fundamentally created by simply declaring (and, where necessary, defining) them. The use of new is reserved for dynamic allocation in the free store and returns a pointer for use; this usage should be rare. You successfully fixed this in your final code snippet.

How do I create an unitialized type accesible to the rest of the class methods, that gets built by the parent objects constructor?

If the member is of a class type, it will always be initialised. You can't have an object that doesn't exist. The closest you can get is to encapsulate a pointer rather than an object:

class Foo {
public:
Foo(int valueBarNeeds)
: _bar(nullptr)
{
// some time later...
_bar = new Bar(valueBarNeeds);
}
private:
Bar* _bar;
};

But this opens up a can of worms as regards memory management and whatnot and, as explained above, you should avoid it unless you really need it. Alternatives include smart pointers but you should still consider sticking with the bog-standard object encapsulation where possible. It should be rare that you need to deliberately leave an object in an invalid state for a time.

C++ How to Initialize member object?

Member variables are initialized in constructor's initialization list (before body) so you need to do that:

B::B(A &a)
: c(a) // Calls constructor C(a) on member c
{}

Do constructors have to initialize member variables in C++?

It really depends on what member variables you have. If you provide a constructor and don't explicitly initialize a variable in the member initialization list, then it will be default initialized. And this is for every variable.

Now, default initialization does something else depending on what variable you have. If you have a builtin type, like int or bool, then it will not be initialized to 0 or any other value, just like if you had:

int value; // it has an indeterminate value

This also applies to arrays. If it is another class, then the default constructor of that class will be called, just like if you had:

struct Foo { /*something*/ };
Foo value; // calls default constructor, i.e. initializes object


Related Topics



Leave a reply



Submit