Jump to content
  • Advertisement
Sign in to follow this  
Deadlybones

C++ instance of class as member of another class

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

Ok, so I'm having some trouble here. I have two different classes set up, each separated in to a .cpp and a .h file. Lets call the first class "First" and the second class "Second"(terrible class names just for the sake of discussion). So in the "First" class(in the header file), one of it's public members is an instance of the "Second" class.

ex. Second second(parameter1, paramater2); //Pseudo-code; barely C++

Now I can instance it in the First.h file if there are no parameters aka "Second"'s default constructor, but there are parameters in my situation, so the .h file won't accept it.

So I don't really know what my options are here. Do I create the instanced member using it's default constructor and just re set it after passing in a better instance of the class? Is there a better way to do this?

If you need anything clarified or have further questions, feel free to ask.

Share this post


Link to post
Share on other sites
Advertisement
If I understand you correctly, you need a constructor initializer list.


struct one
{
one(int a) { }
};

struct two
{
one o;

two() : o(5) { } // call's o's constructor with value 5
};

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!