Jump to content
  • Advertisement
Sign in to follow this  
goldenpanda

Unity Can a C++ constructor invoke another constructor on its own class instance?

This topic is 4848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For example:
class Foo {
   Foo(int i) {...}
   Foo(float f) {
      Foo(42);   // I want to call Foo(int) on the current instance, rather than to create a new instance on the stack
      ...
   }
}

Can I get Foo(float) to invoke Foo(int)? I see a lot of this opportunity in my code, and I hate to factor out constructor code into a member method.

Share this post


Link to post
Share on other sites
Advertisement
That is unfortunately not possible in the current language. What you can do, however, is create a private member function to handle the common tasks and call it from the constructors. You can't use initializer lists in the init function, though.

Share this post


Link to post
Share on other sites
You can at least do


class A
{
public:
A() { *this = A(1); cout << "default" << endl; }
A(int) { cout << "int" << endl; }

};


, but this seems like something that should be possible within the initializer list... maybe derived classes only...

EDIT: See the above post.

Share this post


Link to post
Share on other sites
class Foo {
private:
void Foo Initialize(int i) {...}
public:
Foo( int i ) { Initialize( i ); }
Foo(float f) {
Foo Initialize(42);
...
}
};


Not that much code, unfortunately this is the method you have to use AFAIK.

Share this post


Link to post
Share on other sites
Quote:
Original post by CJH
You can at least do


class A
{
public:
A() { *this = A(1); cout << "default" << endl; }
A(int) { cout << "int" << endl; }

};


, but this seems like something that should be possible within the initializer list... maybe derived classes only...

EDIT: See the above post.


You can only call a base class constructor in the initializer list.

I have tried *this=A(1), and have gotten nasty memory problems. I could be causing it somehow, but I could fix the problem only by factoring out A(int) code into a member method.

Share this post


Link to post
Share on other sites
Quote:
Original post by CJH
Actually, yeah *this = A() is dodgy-eradoo.


Yep, the instance is not construtced until the constructor has completed, so assigning to the instance is probably undefined in the constructor.

Share this post


Link to post
Share on other sites
Perhaps the bridge pattern, also known as the pimpl idiom can help a bit.



class Implementation
{
public:
Implementation(int x);

int m_x;
};

class Interface
{
public:
Interface(int x);
Interface(float y);
Interface(double z);
...
private:

Implementation* pImpl_;

};


Interface::Interface(int x):
pImpl_(new Implementation(x))
{
}

Interface::Interface(float y):
pImpl_(new Implementation(static_cast<int>(y))
{
}


Interface::Interface(double z):
pImpl_(new Implementation(static_cast<int>(z))
{
}








Here the Interface constructors simply use there parameters to intialize an implementation object.

YOu would then use it something like the following



void Interface::DoSomething()
{
pImpl_->DoSomething();
}




Here the Interface class simply delgates all of its functions to the Implementation class.

Another option would be to just use pImpl_ to store the member fields of the structure, and do something like


void Interface::DoSomething()
{
pImpl_->Result = pImpl_->x_ + pImpl_->y;
}

Share this post


Link to post
Share on other sites
Or you can derive Foo from a base class Bar, where Bar has the single constructor that takes arguments, and the Foo contructors forwards the the arguments to the proper Bar constructors.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!