class first
{
public:
first();
~first();
};
class second
{
protected:
second();
~second();
};
class third
{
third();
~third();
};
Question about constructors and destructors
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?
it has nothing to do w/ inheritance. Each destructor is the destructor for each class its in and same with the constructors.
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.
'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.
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
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.
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:
and similarly for a friend.
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.
Ops.
Wasn't sure about that one :)
Haven't programmed using classes lately (as a poor excuse for not knowing).
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}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement