Question about constructors and destructors

Started by
9 comments, last by jods 19 years, 8 months ago
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();
};

Advertisement
it has nothing to do w/ inheritance. Each destructor is the destructor for each class its in and same with the constructors.
______________________________________________________________________________________With the flesh of a cow.
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.
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.
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
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.
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.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Ops.

Wasn't sure about that one :)
Haven't programmed using classes lately (as a poor excuse for not knowing).
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}
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
Ah! Luctus, thanks. I get it now.

This topic is closed to new replies.

Advertisement