protected inheritance modifier

W

Wenjie

Hello!


Suppose I have:

class B {
protected:
virtual ~B();
};

class D: public B {
public:
~D();
};


Is it a good practice to use "protected" in class B?
 
G

Gianni Mariani

Wenjie said:
Hello!


Suppose I have:

class B {
protected:
virtual ~B();
};

class D: public B {
public:
~D();
};


Is it a good practice to use "protected" in class B?

only if you want methods in classes that inherit B be the only methods
able to delete B.
 
S

stephan beal

Wenjie said:
Suppose I have:

class B {
protected:
virtual ~B();
};

class D: public B {
public:
~D();
};


Is it a good practice to use "protected" in class B?

Not an answer to your question, but something to keep i mind:
Making your dtor protected will keep your class from being used in 3rd-party
custom memory-mgt code which calls the dtor directly (as in,
thefoo->~foo()).

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.
 
V

Victor Bazarov

stephan beal said:
Not an answer to your question, but something to keep i mind:
Making your dtor protected will keep your class from being used in 3rd-party
custom memory-mgt code which calls the dtor directly (as in,
thefoo->~foo()).

.... or indirectly, anywhere, as in

{
foo afoo;
...
} // afoo::~foo() is supposed to be called here

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top