construction order of object's members

Mar 27, 2019 at 5:03pm
In which order will the elements of an object get constructed?

E.g., if we have a class with some members and a user-defined constructor like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class FrameRate : private sf::NonCopyable
{
private:
    sf::Text mText;
    sf::Font mFont;
    sf::RenderWindow mWindow;
    sf::Clock mClock;
    sf::Time mTime;
    sf::String mString;
private:
    void update();
    void render();
public:
    FrameRate();
    void run();
};

FrameRate::FrameRate()
: mWindow( sf::VideoMode(800,200), "Framerate" )
, mText()
, mClock()

{
    mFont.loadFromFile("/usr/share/fonts/truetype/ubuntu/UbuntuMono-B.ttf" );
    mString = sf::String("framerate: ");
    mText.setFont(mFont);
    mText.setCharacterSize(100);
    mText.setFillColor(sf::Color(0xAAAAAA));
    mText.setString(mString);
}
Last edited on Mar 27, 2019 at 5:03pm
Mar 27, 2019 at 5:05pm
Members are initialized in the order they are declared. So mText will be initialized first, then mFont, mWindow, mClock, mTime, mString, in that order.
Mar 27, 2019 at 5:29pm
Thank you Peter87!
Mar 27, 2019 at 7:28pm
I wrote a program for testing what Peter87 said:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using std::cerr;

struct A { 
    A(){ cerr << "A()\n"; }
    A(A& a){ cerr << "A(A& a)\n"; }
    ~A(){ cerr << "~A()\n"; }
};
struct B { 
    B(){ cerr << "B()\n"; } 
    ~B(){ cerr << "~B()\n"; } 
};
struct C { 
    C(){ cerr << "C()\n"; } 
    ~C(){ cerr << "~C()\n"; }
};

struct Test {
    // This order destines in which order the objects will get constructed
    // and destructed (destruction order is their reverse order of
    // the construction order).
    A objAA;
    A objA;
    B objB;
    C objC;
    // This order doesn't influence the construction/destruction order.
    Test() : objC(), objB(), objAA(objA), objA(){}
};

int main ()
{
    Test testobj;
}

https://rextester.com/ZYVD92791
output:
A(A& a)
A()
B()
C()
~C()
~B()
~A()
~A()
But this program threw to me the question: Why can at line 22 objAA get constructed although it depends on objA at line 23?
Last edited on Mar 27, 2019 at 8:08pm
Mar 27, 2019 at 8:03pm
Sorry for my last question, at the online compiler were the compiler warnings (per default) disabled. Enabling them throws the warning that objA will be passed uninitialized to objA.
Mar 27, 2019 at 8:04pm
Why can at line 22 objAA get constructed although it depends on objA at line 23?

A good compiler will warn you about this. If you're using GCC or Clang you should at least use the -Wall compiler flag. https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall

This is what GCC says:
test.cpp: In constructor ‘Test::Test()’:
test.cpp:25:7: warning: ‘Test::objC’ will be initialized after [-Wreorder]
     C objC;
       ^~~~
test.cpp:24:7: warning:   ‘B Test::objB’ [-Wreorder]
     B objB;
       ^~~~
test.cpp:27:5: warning:   when initialized here [-Wreorder]
     Test() : objC(), objB(), objAA(objA), objA(){}
     ^~~~
test.cpp:24:7: warning: ‘Test::objB’ will be initialized after [-Wreorder]
     B objB;
       ^~~~
test.cpp:22:7: warning:   ‘A Test::objAA’ [-Wreorder]
     A objAA;
       ^~~~~
test.cpp:27:5: warning:   when initialized here [-Wreorder]
     Test() : objC(), objB(), objAA(objA), objA(){}
     ^~~~
Topic archived. No new replies allowed.