By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,041 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,041 IT Pros & Developers. It's quick & easy.

deriving a class...

P: n/a
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?

Jun 19 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Shraddha wrote:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
Yes. See the FAQ.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 19 '07 #2

P: n/a
On Jun 19, 11:10 am, Shraddha <shraddhajosh...@gmail.comwrote:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
Yes you can but you have to do some tweaks with your class's
construction and with inheritance.

class Usable;

class Usable_lock {
friend class Usable;
private:
Usable_lock() {}
Usable_lock(const Usable_lock&) {}
};

class Usable : public virtual Usable_lock {
// ...
public:
Usable();
Usable(char*);
// ...
};

Usable a;

class DD : public Usable { };

DD dd; // error: DD::DD() cannot access
// Usable_lock::Usable_lock(): private member

You can check more about it at
http://www.research.att.com/~bs/bs_f...#no-derivation

Regards,
Sarath
http://sarathc.wordpress.com/

Jun 19 '07 #3

P: n/a
"Shraddha" <sh*************@gmail.comwrote in message
news:11*********************@o11g2000prd.googlegro ups.com...
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
3 ways, shown in the FAQ:
http://www.parashift.com/c++-faq-lit...html#faq-23.11
Jun 19 '07 #4

P: n/a
Shraddha wrote:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
Technically, yes. See:

http://www.research.att.com/~bs/bs_f...#no-derivation

A slightly different approach uses a protected constructor:

/*
| This defines the macro
|
| FINAL
|
| to be used to prevent derivation:
|
| struct X : FINAL {};
|
| struct Y : X {}; // declaring variables of this type won't work
*/
// credits:
/*
Found in news.lang.c++.moderated (Gennaro Prota)
see:
http://groups.google.com/group/comp....3980680a2f7136
*/

class protected_constructor {
protected:

protected_constructor ( void ) {}

}; // protected_constructor

#define FINAL private virtual protected_constructor
struct X : FINAL {};

struct Y : X {}; //

int main ( void ) {

X x; // fine.

Y y; // compile time error.

}
_However_ (!!!), as explained in Stroustrups FAQ, it is highly doubtfull
that you have a valid reason to do something nasty like this. Keep in mind
that you are probably unable to predict the reasons that clients of your
code have to derive from your classes. Note that C++ supports multiple
programming styles, where different reasons for derivation are considered
valid. I doubt that you have sufficient information to decide for the
client code programmers that derivation is wrong. That is their
responsibility. Trust them; and if they screw up, it's their problem. That
said, there might be cases where you have a valid reason, although I don't
know of any convincing examples.
Best

Kai-Uwe Bux
Jun 19 '07 #5

P: n/a
On Jun 19, 4:10 am, Shraddha <shraddhajosh...@gmail.comwrote:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough. In fact, unless you're in a very
strange situation, just not documenting how to use it as a base
class should be sufficient.

If it's not, you've got a serious problem, no matter what you
do.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 19 '07 #6

P: n/a
On Tue, 19 Jun 2007 09:06:07 -0000, James Kanze wrote:
>On Jun 19, 4:10 am, Shraddha <shraddhajosh...@gmail.comwrote:
>Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?

Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough. In fact, unless you're in a very
strange situation, just not documenting how to use it as a base
class should be sufficient.

If it's not, you've got a serious problem, no matter what you
do.
BTW, to clarify a too common misunderstanding, one may render any
derived class non-instantiable, not preventing derivation itself (or
usage of accessible static members in the derived class).

class final
{
protected:
final();
};

#define FINAL_CLASS private virtual final

// usage:
class my_class : FINAL_CLASS
{};

Why one might want to do that is another story :-)

--
Gennaro Prota -- Need C++ expertise? I'm available
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
Jun 19 '07 #7

P: n/a

"James Kanze" <ja*********@gmail.comwrote in message
news:11********************@u2g2000hsc.googlegroup s.com...
On Jun 19, 4:10 am, Shraddha <shraddhajosh...@gmail.comwrote:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
"Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough. "

A non-virtual destructor would be telltale "documentation".

John

Jun 22 '07 #8

P: n/a
On Jun 22, 7:57 am, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
"James Kanze" <james.ka...@gmail.comwrote in message
news:11********************@u2g2000hsc.googlegroup s.com...
On Jun 19, 4:10 am, Shraddha <shraddhajosh...@gmail.comwrote:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
"Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough. "
A non-virtual destructor would be telltale "documentation".
Not always. What about std::exception?

In fact, most of the time, I think the opposite applies. Unless
the class is documented explicitly to be a base class, you
shouldn't derive from it.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 22 '07 #9

P: n/a

"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
On Jun 22, 7:57 am, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
"James Kanze" <james.ka...@gmail.comwrote in message
news:11********************@u2g2000hsc.googlegroup s.com...
On Jun 19, 4:10 am, Shraddha <shraddhajosh...@gmail.comwrote:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
"Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough. "
A non-virtual destructor would be telltale "documentation".
"Not always. What about std::exception?"

It's destructor is declared virtual in my implementation.

"In fact, most of the time, I think the opposite applies. Unless
the class is documented explicitly to be a base class, you
shouldn't derive from it."

John

Jun 22 '07 #10

P: n/a
JohnQ wrote:
>
"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
On Jun 22, 7:57 am, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
>"James Kanze" <james.ka...@gmail.comwrote in message
>news:11********************@u2g2000hsc.googlegrou ps.com...
On Jun 19, 4:10 am, Shraddha <shraddhajosh...@gmail.comwrote:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
>"Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough. "
>A non-virtual destructor would be telltale "documentation".

"Not always. What about std::exception?"

It's destructor is declared virtual in my implementation.
Nonetheless, the standard library has some examples of template classes
designed for derivation that do just fine without virtual destructors.
Think of unary_function<>, binary_function<>, or iterator<>.

Derivation in C++ is a concept orthogonal to the various programming styles
supported by the language. In the context of policy based design,
derivation from classes is often done without the need for virtual
destructors. The slogan that classes to be derived from need virtual
destructors is firmly rooted in the OO style and of somewhat limited scope.
Best

Kai-Uwe Bux
Jun 22 '07 #11

P: n/a
On Jun 22, 7:33 pm, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
"James Kanze" <james.ka...@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
On Jun 22, 7:57 am, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
"James Kanze" <james.ka...@gmail.comwrote in message
news:11********************@u2g2000hsc.googlegroup s.com...
On Jun 19, 4:10 am, Shraddha <shraddhajosh...@gmail.comwrote:
Can I stop people by deriving my class?
I mean I don't want my class to be as a base class...
Can I do that?
"Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough. "
A non-virtual destructor would be telltale "documentation".
"Not always. What about std::exception?"
It's destructor is declared virtual in my implementation.
Oops. I meant std::iterator.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 22 '07 #12

P: n/a
On Jun 19, 4:06 am, James Kanze <james.ka...@gmail.comwrote:
Yes and no. Unless you're in a very strange situation, just
documenting the fact that the class isn't designed to be used as
a base class should be enough.
By the same logic, just documenting that a person should not call a
particular method under any circumstances should be enough. Therefore
the "private" and "protected" keywords can safely be removed from the
language.

Declaring the destructor non-virtual is, unfortunately, the best
option we have to ensure people don't derive from classes unless you
want to seriously uglify your code. A keyword similar to
"final" (Java) or "sealed" (C#) would definitely be nice though, and
would probably see considerable use.

Jun 22 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.