Jump to content
  • Advertisement
Sign in to follow this  
Drakkcon

Question about constructors and destructors

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

What would be the difference between these three constructors and destructor? What difference does it's accessibility make with the (con/de)structor? Does it involve inheritance?
class first
{
 public:
 first();
 ~first();
};

class second
{
 protected:
 second();
 ~second();
};

class third
{
 third();
 ~third();
};

Share this post


Link to post
Share on other sites
Advertisement
it has nothing to do w/ inheritance. Each destructor is the destructor for each class its in and same with the constructors.

Share this post


Link to post
Share on other sites
This actually has nothing to do with ctors/dtors; the "level of access" can be applied to any method or data member of a class.

'public' items are accessible from anywhere.
'protected' items are accessible only from this class, its friends, and classes which derive from it.
'private' items are accessible only from this class and its friends.

Share this post


Link to post
Share on other sites
You can also have several construcotrs/destructors within one class due to the overloading feature.
This means that several constructors have different parameters.

Maybe you should look into that at a later stage, I don't intend to confuse you, just thought you should know that.

Share this post


Link to post
Share on other sites
Ctors:
level access works in the same way as for "normal" methods.

Using your example (code supposed to be outside the class):

first firstInstance = new first();
is ok.

second secondInstance = new second();
is not since the second ctor is not accessible.

Example of private ctor usefulness: Singleton pattern. Declaring the default ctor private guarantees that noone (except the class itself) will ever instantiate the singleton class.

Dtors:
usually it makes no differences. But you can call a destructor explicitly (seldom used). In that case, the level access restrictions apply as usual.

edit> dtor answer

Share this post


Link to post
Share on other sites
l3mon>

No you cannot have several destructors in one class. A dtor takes no arguments, which implies that you cannot overload it.

But the statement is true for ctor of course.

Share this post


Link to post
Share on other sites
In your particular example:

The 'first' class can be created by anyone.
The 'second' class can only be created by itself, a friend or a deriving class.
The 'third' class can only be created by itself or a friend. This might sound odd, but it can be useful. You can use a static member that creates the type for you like so:

class third
{
third();
~third();
public:
static third* create()
{
return new third;
}
};


and similarly for a friend.

Share this post


Link to post
Share on other sites
Ops.

Wasn't sure about that one :)
Haven't programmed using classes lately (as a poor excuse for not knowing).

Share this post


Link to post
Share on other sites
Quote:
Original post by jods
usually it makes no differences. But you can call a destructor explicitly (seldom used). In that case, the level access restrictions apply as usual.


Not sure if that's what you meant, but the level access determines who's able to call the delete operator on a class. e.g.


class Foo
{
public:
Foo( void ) {}

protected:
~Foo( void ) {}
};

class Bar : public Foo
{
public:
Bar( void ) {}
~Bar( void ) {}
};

int main( void )
{
Foo *f = new Foo();
Bar *b = new Bar();

delete f; // compiler error
delete b; // ok
}

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!