Can a C++ constructor call another constructor?

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0
For example, let's say we have a default constructor like:

MyClass::MyClass()
{
ptrStuff = NULL;
}


And another constructor that calls the above constructor:

MyClass::MyClass(int a, int b)
{
MyClass();

// do stuff
}

I'm running into problems when I do the above. The ptrStuff is a private class variable and it doesn't seem to be set to NULL after returning from the default constructor. I thought I used to be able to do this. I'm working with C++ at work but I recently took a Java class and this has gotten me confused about C++ stuff.
 

Warthog912

Golden Member
Jun 17, 2001
1,653
0
76
/backs outta thread

apologize, I was gonna say something smart, but this is a serious thread

/backs outta thread
 

MAME

Banned
Sep 19, 2003
9,281
1
0
I've been using Java a lot more than C++ too but I think you can do it. I don't see why you'd have to though, makes no sense. Just put the code from the second constructor in to the first one and you have no more function overhead to worry about
 

MichaelD

Lifer
Jan 16, 2001
31,528
3
76
Originally posted by: Warthog912
/backs outta thread

apologize, I was gonna say something smart, but this is a serious thread

/backs outta thread

Yes, it is. And it's also in the WRONG FORUM, which makes it fair game for Thread Pooping! *poop*



Stormy,

Not EVEN a Constructor-Constructor, Deconstructor-caller could help your dumb ass crawl out from under that lichen-infested rock you somehow survive under. Shouldn't you be hitting on 5th grade girls or something?
 

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0
Originally posted by: MAME
I've been using Java a lot more than C++ too but I think you can do it. I don't see why you'd have to though, makes no sense. Just put the code from the second constructor in to the first one and you have no more function overhead to worry about

It kind of makes sense from a maintenance standpoint though. I like to avoid repeating code in different places so if I have to change something I don't have to change it in all the different places.
 

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0
Originally posted by: MichaelD
Originally posted by: Warthog912
/backs outta thread

apologize, I was gonna say something smart, but this is a serious thread

/backs outta thread

Yes, it is. And it's also in the WRONG FORUM, which makes it fair game for Thread Pooping! *poop*



Stormy,

Not EVEN a Constructor-Constructor, Deconstructor-caller could help your dumb ass crawl out from under that lichen-infested rock you somehow survive under. Shouldn't you be hitting on 5th grade girls or something?

I'm gonna call the police on you for playing your stereo too loud!
 

manly

Lifer
Jan 25, 2000
13,544
4,224
136
No you can't chain constructors like in Java.

Originally posted by: ed21x
yeah, that or make it a global variable.
What kind of lame answer is that?
 

MAME

Banned
Sep 19, 2003
9,281
1
0
Originally posted by: StormRider
Originally posted by: MAME
I've been using Java a lot more than C++ too but I think you can do it. I don't see why you'd have to though, makes no sense. Just put the code from the second constructor in to the first one and you have no more function overhead to worry about

It kind of makes sense from a maintenance standpoint though. I like to avoid repeating code in different places so if I have to change something I don't have to change it in all the different places.

Haha, that sure does sound like Java!

Anyway, I think it would work as the pc just hops to whatever's next and gets the job done. But I guess it may not work, only due to the creators of the language specifically excluding that as an option. You can make a very simple program and see if it works. (I would for you but CodeWarrior isn't installed on my computer for some reason....)
 

MAME

Banned
Sep 19, 2003
9,281
1
0
Originally posted by: manly
No you can't chain constructors like in Java.

Originally posted by: ed21x
yeah, that or make it a global variable.
What kind of lame answer is that?

Ok, I guess I was wrong then. I don't see why they excluded it but oh well. There's no need to do it imo anyway, unless the code is really long.
 

manly

Lifer
Jan 25, 2000
13,544
4,224
136
Originally posted by: MAME
Originally posted by: manly
No you can't chain constructors like in Java.

Originally posted by: ed21x
yeah, that or make it a global variable.
What kind of lame answer is that?

Ok, I guess I was wrong then. I don't see why they excluded it but oh well. There's no need to do it imo anyway, unless the code is really long.
You can use default values for parameters, which would achieve the same effect (from the caller's perspective). But it's not done by constructor chaining.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
ok...let me see if I get you right.

You're trying to make it so that when you call the alternate constructor, it still sets ptrstuff = NULL, right?

Your coding won't even do that, if its even possible. it will create a new object with the default constructor, but then not set anything to it. There are ways you could do it by calling a default constructor, but it would be much more simple to just copy the code from the default.
 

Gibson486

Lifer
Aug 9, 2000
18,378
2
0
I remember my prof talking about an issue like this in class (one of the only things i actually learned in that class). It's possible in Java, but not C++.
 

acemcmac

Lifer
Mar 31, 2003
13,712
1
0
Originally posted by: PipBoy
conjunction junction, what's your function....

HAHAHA.

I'd definitley lend a hand, but I spent the constructor lecture thursday night hitting on and then going out with only worthwhile looking chick in the class.... :beer:
 

Rahminator

Senior member
Oct 11, 2001
726
0
0
Nope, the MyClass() in the nondefault constructor will just create another instance of MyClass class. Since you're not storing a reference to it, the object is an unnamed temp that you have no way to access and will get destroyed when it goes outta scope.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
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
}

 

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0
Thanks for all the help guys. I just copied the code from the default constructor and now it works. Taking that Java class really messed me up on C++. And to top it off, now I like Java better!
 

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0
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.
 

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0
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.
 

PowerMacG5

Diamond Member
Apr 14, 2002
7,701
0
0
Originally posted by: manly
No you can't chain constructors like in Java.

Originally posted by: ed21x
yeah, that or make it a global variable.
What kind of lame answer is that?

Thinking the same thing here. I hate when people think that the answer to every programming problem is a global variable.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
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.

StormRider , even a better way to help write your code is to do the following:


class Foo {

public:

Foo() : Foo(0,0)
{
}

Foo(int data) : Foo(data,0)
{
}

Foo(int monkey, int apple)
{
a = monkey;
b = NULL;
c = apple;

}

private:
int a;
void * b;
int c;
}


If you do it like this, all your code goes into the most specialized version of your constructor and you set default values when the user doesn't specify the paramters. that way you only have to fix bugs in one place. this doesnt just apply to constructors but normal function overloading as well.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
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.


hmm i know it works for calling the base class constructor, i thought it would work for peer too.

lemme try it, give me a sec.