Jump to content
  • Advertisement
Sign in to follow this  

simple question about constructor / destructor

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

Hi, I would like to know if when placing a class inside a struct, and then, when creating a reference of that struct, the constructor / destructor of the class inside, is called: class TARGET { whatever... } struct TEST1 { TARGET x; } TEST1* y = new TEST1; delete y;

Share this post


Link to post
Share on other sites
Advertisement
I wouldn't have thought so - the structure itself doesn't have a constructor as such, although one can be provided. The best way to find out is to write a test program (and quicker than posting here!):

class Test
{
public:
Test (void) {cout << "Constructor" << endl;}
~Test (void) {cout << "Destructor" << endl;}
};

struct S
{
Test t;
}

main ()
{
new S;
}

But the real question is: why would you use a structure with a class inside it?

Skizz

Share this post


Link to post
Share on other sites
Answer to the original question: Yes.

Actually, the struct and class keywords are synonyms with the only exception that struct has public access as default for its members.

Share this post


Link to post
Share on other sites
And as a reply to Skizz: The TEST1 struct does have a constructor. The compiler generates one as it is needed to make sure that the member x's constructor is called.

Share this post


Link to post
Share on other sites
Quote:
Original post by vilcans
Answer to the original question: Yes.

Actually, the struct and class keywords are synonyms with the only exception that struct has public access as default for its members.


And it derives publicly by default wheras classes derive privately


class Base {};
class Derived : Base {//private inheritance of Base
};



struct Base {};
struct Derived : Base {//public inheritance of Base
};



As Bjarne says in his book

Quote:

struct s {...


is simply shorthand for

class s { public:...

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!