By Logical Fallacy


2012-10-17 04:33:36 8 Comments

I've got a class that has a couple of objects as member variables. I don't want the constructors for these members to be called when declared, so I'm trying to hang onto a pointer to the object explicitly. I have no idea what I'm doing. o_O

On StackOverflow, I seem to be able to find other examples of object member variables, but usually the constructor is called immediately, like this:

class MyClass {
    public:
        MyClass(int n);
    private:
        AnotherClass another(100); // this constructs AnotherClass right away!
};

But I want the MyClass constructor to call the AnotherClass constructor. Here's what my code looks like:

BigMommaClass.h

#include "ThingOne.h"
#include "ThingTwo.h"

class BigMommaClass {

        public:
                BigMommaClass(int numba1, int numba2);

        private:
                ThingOne* ThingOne;
                ThingTwo* ThingTwo;
};

BigMommaClass.cpp

#include "BigMommaClass.h"

BigMommaClass::BigMommaClass(int numba1, int numba2) {
        this->ThingOne = ThingOne(100);
        this->ThingTwo = ThingTwo(numba1, numba2);
}

Here's the error I'm getting when I try to compile:

g++ -Wall -c -Iclasses -o objects/BigMommaClass.o classes/BigMommaClass.cpp
In file included from classes/BigMommaClass.cpp:1:0:
classes/BigMommaClass.h:12:8: error: declaration of âThingTwo* BigMommaClass::ThingTwoâ
classes/ThingTwo.h:1:11: error: changes meaning of âThingTwoâ from âclass ThingTwoâ
classes/BigMommaClass.cpp: In constructor âBigMommaClass::BigMommaClass(int, int)â:
classes/BigMommaClass.cpp:4:30: error: cannot convert âThingOneâ to âThingOne*â in assignment
classes/BigMommaClass.cpp:5:37: error: â((BigMommaClass*)this)->BigMommaClass::ThingTwoâ cannot be used as a function
make: *** [BigMommaClass.o] Error 1

Am I using the right approach but the wrong syntax? Or should I be coming at this from a different direction?

5 comments

@Guy Avraham 2018-12-20 16:55:58

I'm (also, as others mentioned) aware to the fact that this question is old, yet I wanted to point something regarding the first (and great) answer from @chris who proposed a solution to the situation where the class members are held as a "true composite" members (i.e.- NOT as pointers NOR references). The note is a bit large so I will demonstrate it here with some sample code.

When you chose to hold the members as I mentioned, you have to keep in mind also these two things:

1) For every "composed object" that DOES NOT have a default ctor - you MUST initialize it in the initialization list of ALL the ctor's of the "father" class (i.e.- BigMommaClass or MyClass in the original examples and MyClass in the code below), in case there are several (see InnerClass1 in the example below). Meaning, you can "comment out" the m_innerClass1(a) and m_innerClass1(15) ONLY if you enable the InnerClass1 default ctor.

2) For every "composed object" that DOES have a default ctor - you MAY initialize it within the initialization list, but it will work also if you chose not to (see InnerClass2 in the example below).

See sample code (complied under Ubuntu 18.04 with g++ version 7.3.0):

#include <iostream>

using namespace std;

class InnerClass1
{
    public:
        InnerClass1(int a) : m_a(a)
        {
            cout << "InnerClass1::InnerClass1 - set m_a:" << m_a << endl;
        }

        /* No default cotr
        InnerClass1() : m_a(15)
        {
            cout << "InnerClass1::InnerClass1() - set m_a:" << m_a << endl;
        }
        */

        ~InnerClass1()
        {
            cout << "InnerClass1::~InnerClass1" << endl;
        }

    private:
        int m_a;
};

class InnerClass2
{
    public:
        InnerClass2(int a) : m_a(a)
        {
            cout << "InnerClass2::InnerClass2 - set m_a:" << m_a << endl;
        }

        InnerClass2() : m_a(15)
        {
            cout << "InnerClass2::InnerClass2() - set m_a:" << m_a << endl;
        }

        ~InnerClass2()
        {
            cout << "InnerClass2::~InnerClass2" << endl;
        }

    private:
        int m_a;
};

class MyClass
{
    public:
        MyClass(int a, int b) : m_innerClass1(a), /* m_innerClass2(a),*/ m_b(b)
        {
            cout << "MyClass::MyClass(int b) - set m_b to:" << m_b << endl;
        }

         MyClass() : m_innerClass1(15), /*m_innerClass2(15),*/ m_b(17) 
        {
            cout << "MyClass::MyClass() - m_b:" << m_b << endl;
        }

        ~MyClass()
        {
            cout << "MyClass::~MyClass" << endl;
        }

    private:
        InnerClass1 m_innerClass1;
        InnerClass2 m_innerClass2;
        int m_b;
};

int main(int argc, char** argv)
{

    cout << "main - start" << endl;

    MyClass obj;

    cout << "main - end" << endl;
    return 0;
}

@Ivan 2018-08-30 12:00:29

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.

@patmanpato 2015-12-03 23:28:43

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.

@Yuushi 2012-10-17 04:39:57

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

@Logical Fallacy 2012-10-17 04:42:18

thanks. So should I have ThingOne* t1 or should I just use ThingOne t1?

@chris 2012-10-17 04:42:40

@DavidEnglund, Use the latter. Unnecessary dynamic allocation is bad. If you really needed it, a smart pointer would have been the way to go.

@Logical Fallacy 2012-10-17 04:43:53

@chris, thanks. I was using the pointers in an effort to prevent the constructors from being called right away. I think that's cleared up now, though.

@chris 2012-10-17 04:45:19

@DavidEnglund, I just want to point out that specifying arguments at the declaration in the class is C++11 only anyway, but even it doesn't call it right away. When that object is initialized, that will just be the default if you don't override it. Look up in-class member initialization for more info on that. I think you also have to use uniform initialization to do that as well if it isn't just an implicit constructor with one parameter that you can get with ThingOne thingOne = 100;.

@Yuushi 2012-10-17 05:11:27

@DavidEnglund As others have said, dynamic allocation is unnecessary in this case. There are use cases for it however (pimpl idiom, polymorphic classes, only requiring forward declarations to help reduce compile times etc).

@Logical Fallacy 2012-10-17 05:13:26

@Yuushi, like for lazy loading?

@Yuushi 2012-10-17 05:34:56

@DavidEnglund That's a separate topic again, but yes, you could use it for that in principle.

@chris 2012-10-17 04:39:09

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

@Logical Fallacy 2012-10-17 05:11:50

It's still bothering me that with this syntax, I can't do any real work in the constructor to pass onto the other constructors. Constructors should probably be lightweight, so I'm having trouble coming up with a real-world example, but I find myself wishing the syntax looked more like this.thingOne = new ThingOne(100); because that just offers more flexibility. But I digress.

@jigglypuff 2018-02-09 04:39:37

Does thingOne and thingTwo have to be initialised in BigMommaClass's constructor? Or can it be done in some other function which is called after?

@chris 2018-02-09 13:59:19

@nobism, For initializaion rather than assignment, it has to be done in a constructor. You are allowed to do something like : thingOne(calculateThing1(numba1, numba2)). In rare cases, you might need initialization, but have no way to do it in the initializer list. In that case, it's possible to use something like std::optional or ts::deferred_construction. However, a lot of thinking would have to go into that decision.

Related Questions

Sponsored Content

37 Answered Questions

17 Answered Questions

[SOLVED] How to initialize private static members in C++?

16 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 490303 View
  • 1726 Score
  • 16 Answer
  • Tags:   c++ unix profiling

21 Answered Questions

[SOLVED] How do I call one constructor from another in Java?

  • 2008-11-12 20:10:19
  • ashokgelal
  • 807112 View
  • 2135 Score
  • 21 Answer
  • Tags:   java constructor

10 Answered Questions

[SOLVED] Throwing exceptions from constructors

17 Answered Questions

[SOLVED] Virtual member call in a constructor

15 Answered Questions

[SOLVED] Can I call a constructor from another constructor (do constructor chaining) in C++?

  • 2008-11-21 09:43:08
  • Stormenet
  • 433277 View
  • 876 Score
  • 15 Answer
  • Tags:   c++ constructor

3 Answered Questions

[SOLVED] Which new is execute first -- in constructor or out?

  • 2011-04-17 07:12:10
  • Lai Yu-Hsuan
  • 708 View
  • 3 Score
  • 3 Answer
  • Tags:   java constructor

Sponsored Content