Sign in to follow this  
Drakkcon

Question about constructors and destructors

Recommended Posts

Drakkcon    612
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
Ainokea    435
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
Zahlman    1682
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
l3mon    128
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
jods    367
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
jods    367
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
joanusdmentia    1060
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
l3mon    128
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
Luctus    584
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
jods    367
Luctus>
yes, after reading it again I admit that it wasn't very clear. In fact I was thinking of explicit destructor call. For example:

eagle.~Bird();

As I said it's a possibility that is very seldom used (but exists).

Have a nice day,
jods

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this