• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Can a C++ constructor call another constructor?

StormRider

Diamond Member
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.
 
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
 
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?
 
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.
 
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!
 
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....)
 
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.
 
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.
 
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.
 
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++.
 
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:
 
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.
 
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 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!
 
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.
 
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.
 
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.
 
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.
 
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.
 
Back
Top