Initializer list issue

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

I recently had an issue that I'll try to condense for your reviewing
pleasure:

class A
{
protected:
my_type Member;

public:
A() {/* initialize Member */}
};

class B : public A
{
private:
my_other_type MyMember;

public:
B() : A(), MyMember(Member) {}
};

This compiled, but crashed at run time. Was it because the order in
which the actions in the initializer list are taken is unspecified?
 
J

John Harrison

Christopher Benson-Manica said:
I recently had an issue that I'll try to condense for your reviewing
pleasure:

class A
{
protected:
my_type Member;

public:
A() {/* initialize Member */}
};

class B : public A
{
private:
my_other_type MyMember;

public:
B() : A(), MyMember(Member) {}
};

This compiled, but crashed at run time. Was it because the order in
which the actions in the initializer list are taken is unspecified?

No the order is defined but its a little different from what some people
expect.

Simplyfying a little; the order is that all bases are initialised first in
the order that they appear in the class declaration, not in the order that
they appear in the initialiser list. E.g.

class A : public B, public C
{
A() : C(), B() {} // B is initialised before C
};

Then all the members are initialised, again this is in the order that they
are in the class declaration, not in the initialiser list. E.g.

class A
{
A() : c(), b() {} // b is initialised before c
B b;
C c;
};

john
 
J

Jeff Schwab

Christopher said:
I recently had an issue that I'll try to condense for your reviewing
pleasure:

class A
{
protected:
my_type Member;

public:
A() {/* initialize Member */}
};

class B : public A
{
private:
my_other_type MyMember;

public:
B() : A(), MyMember(Member) {}
};

This compiled, but crashed at run time. Was it because the order in
which the actions in the initializer list are taken is unspecified?

Nope. Sorry, no way to tell from this code what caused the crash.
 
C

Christopher Benson-Manica

Jeff Schwab said:
Nope. Sorry, no way to tell from this code what caused the crash.

Well, I guess I'll tentatively blame my compiler (again), since I
sidestepped whatever problem there was by making the derived class
member a pointer and calling new in the constructor. I really didn't
see any reason why that should have made a difference, but it did...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top