Jump to content
  • Advertisement
Sign in to follow this  
Raeldor

Call default constructor from parameterized constructor

This topic is 4827 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

Can I call my classes own default constructor from within a parameterized constructor of the same class? I tried doing it, but it seems to think I am creating a new object. I have code in my default constructor that I don't want to duplicate in the parameterized constructor. I could create a function and call it from both, but I thought it would be a better solution to just call the default constructor if possible. Seems this should be a common thing, can someone point me in the right direction please. Thanks Rael

Share this post


Link to post
Share on other sites
Advertisement
You cannot call a constructor of the class you are implementing from a constructor. You can call specific base class constructors, however, so you can consider refactoring that in a base class.

Share this post


Link to post
Share on other sites
The usual design pattern is to factor out the necessary code to a private helper function, and call it from both places.

However, you should first consider trying to do as much as possible via initializer lists. :)

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!