Jump to content
  • Advertisement
Sign in to follow this  
pixelartist

Struct Initialization Within Struct

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

C++

Is it possible to call a constructor with parameters for a struct within another struct? (Something like this, though VC++ is highlighting the "2" in myA and saying it wants a type specifier, so this is obviously wrong).

[source lang="cpp"]struct A{
int iSize;
A(int x = 10) { iSize = x; }
}; // A

struct B{
A myA(2);
}; // B[/source]

Essentially I want a struct that has a standardized initial Size member, but I also want to be able to specify a different Size member for it when it is itself a member of other structs.

Share this post


Link to post
Share on other sites
Advertisement
Yes, but unfortunately C++ doesn't allow you to conveniently do the initialization like you do on line 7. You'll have to do it in a constructor, like as follows:

[source lang="cpp"]struct A{
int iSize;
A(int x = 10) { iSize = x; }
}; // A

struct B{
A myA;

B()
:myA(2)
{
}
}; // B[/source]

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!