Can a C++ constructor call another constructor?

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

manly

Lifer
Jan 25, 2000
13,306
4,084
136
Originally posted by: StormRider
Originally posted by: StormRider
Originally posted by: Ameesh
yes you can have one constructor call another.

just add it to the end of your declaration of the specialized constructor

MyClass::MyClass(int a, int b) : MyClass()
{
//do stuff
}


Thanks! I like that approach better than having the same code in 2 different places.

Oops, that didn't work. Gave me the following error:

C:\Program Files\Microsoft Visual Studio\MyProjects\myCode.cpp(37) : error C2614: 'MyClass' : illegal member initialization: 'MyClass' is not a base or member

I think that only works if you want to call the superclasses' default constructor.
Exactly. See my post for the correct answer. ;)
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
ok this builds fine:


StormRiderTest.h:

class StormRiderTest
{
public:
StormRiderTest(void);
StormRiderTest(int apple);
StormRiderTest(int apple, void * monkey);
~StormRiderTest(void);
private:
int privateA;
void * privateB;
};



StormRiderTest.cpp:

StormRiderTest::StormRiderTest(void)
{
StormRiderTest(0,NULL);
}

StormRiderTest::StormRiderTest(int apple)
{
StormRiderTest(apple,NULL);
}

StormRiderTest::StormRiderTest(int apple, void * monkey)
{
this->privateA = apple;
this->privateB = monkey;
}


StormRiderTest::~StormRiderTest(void)
{
}

and that does exactly what you want it to do (i tested it and it works)
 

vtqanh

Diamond Member
Jan 4, 2001
3,100
0
76
Originally posted by: Ameesh
ok this builds fine:


StormRiderTest.h:

class StormRiderTest
{
public:
StormRiderTest(void);
StormRiderTest(int apple);
StormRiderTest(int apple, void * monkey);
~StormRiderTest(void);
private:
int privateA;
void * privateB;
};



StormRiderTest.cpp:

StormRiderTest::StormRiderTest(void)
{
StormRiderTest(0,NULL);
}

StormRiderTest::StormRiderTest(int apple)
{
StormRiderTest(apple,NULL);
}

StormRiderTest::StormRiderTest(int apple, void * monkey)
{
this->privateA = apple;
this->privateB = monkey;
}


StormRiderTest::~StormRiderTest(void)
{
}

and that does exactly what you want it to do (i tested it and it works)

Nope
Your code compiled and ran fine. But it did not do what StormRider wanted it to do.
Do some testing and you will see what i mean.
The call StormRiderTest(0, NULL) in the first constructor basically creates a temporary object. It will be destroyed right after the call is finished. It has no effect on the object that is calling it. (put a cout << "destroying object"; in the destructor and you will see better how this works)
 

manly

Lifer
Jan 25, 2000
13,306
4,084
136
We don't think that works, but even if it does, the constructors should be consolidated to just:

StormRiderTest::StormRiderTest(int apple = 0, void * monkey = NULL):
privateA(apple),
privateB(NULL)
{
}
 

vtqanh

Diamond Member
Jan 4, 2001
3,100
0
76
Originally posted by: manly
We don't think that works, but even if it does, the constructors should be consolidated to just:

StormRiderTest::StormRiderTest(int apple = 0, void * monkey = NULL):
privateA(apple),
privateB(NULL)
{
}

well, ameesh was trying to answer the question "Can a C++ constructor call another constructor."
 

manly

Lifer
Jan 25, 2000
13,306
4,084
136
Originally posted by: vtqanh
Originally posted by: manly
We don't think that works, but even if it does, the constructors should be consolidated to just:

StormRiderTest::StormRiderTest(int apple = 0, void * monkey = NULL):
privateA(apple),
privateB(NULL)
{
}

well, ameesh was trying to answer the question "Can a C++ constructor call another constructor."
And obviously, my point is that default arguments are the way to achieve the same effect.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: manly
Originally posted by: vtqanh
Originally posted by: manly
We don't think that works, but even if it does, the constructors should be consolidated to just:

StormRiderTest::StormRiderTest(int apple = 0, void * monkey = NULL):
privateA(apple),
privateB(NULL)
{
}

well, ameesh was trying to answer the question "Can a C++ constructor call another constructor."
And obviously, my point is that default arguments are the way to achieve the same effect.

yep, i made the goof, now i know why our coding standards calls for a virtual init function to be called after construction of a class. :eek:

the default argument method is probably the best way to handle this