A sterile class?

V

Victor Bazarov

Shuckey said:
Is it possible to define a sterile class in C++? That is, a class that
cannot be inherited from?
Yes. See FAQ, please. And, please, ALWAYS see FAQ before posting.
 
S

Shuckey

Hello.

Is it possible to define a sterile class in C++? That is, a class that
cannot be inherited from?

I don't have an exact problem which would call for such a class. To be
quite frank, I can not even think of a concrete example where this would be
an issue. I'm just curious.

TIA,
Hrvoje Bratanic AKA Shuckey AKA Crtowat AKA No-Fun
 
G

Gianni Mariani

Shuckey said:
Hello.

Is it possible to define a sterile class in C++? That is, a class that
cannot be inherited from?

I don't have an exact problem which would call for such a class. To be
quite frank, I can not even think of a concrete example where this would be
an issue. I'm just curious.

Well kinda using virtual inheritance.

class Steralizer
{
friend class Sterile;
Steralizer() {} // private
};


class Sterile : Steralizer
{

...
};


Attempts to inherit from Sterile will result in access errors to
Steralizer's constructor.

I have not figured out a way to make Steralizer a template so you can write:

class A : Steralizer<A>
{
....
};

IIRC there is a prob with defining a class as a friend in a template.
 
K

Kai-Uwe Bux

Gianni said:
Well kinda using virtual inheritance.

class Steralizer
{
friend class Sterile;
Steralizer() {} // private
};


class Sterile : Steralizer
{

...
};


Attempts to inherit from Sterile will result in access errors to
Steralizer's constructor.

I have not figured out a way to make Steralizer a template so you can
write:

class A : Steralizer<A>
{
...
};

IIRC there is a prob with defining a class as a friend in a template.

Yes, the problem is in [7.1.5.3/2]

... If the identifier resolves to a typedefname or a template
type-parameter, the elaborated-type-specifier is ill-formed. [Note: this
implies that, within a class template with a template type-parameter T,
the declaration

friend class T;

is ill-formed. ]


But (a) there is some indication on n2134 that this provision will go away
and (b) you can use a macro:


/*
| 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...7136?lnk=gst&q=sealed&rnum=3#f63980680a2f7136
*/

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.

}



Best

Kai-Uwe Bux
 
G

Gianni Mariani

Gianni said:
Well kinda using virtual inheritance.

class Steralizer
{
friend class Sterile;
Steralizer() {} // private
};


class Sterile : Steralizer

That should be:

class Sterile : virtual Steralizer
 
S

Shuckey

What hath BobR wrought:

It's a song lyric. Just sing along.
[ "Take a walk on the wild side", I think. (?) ]

Yes. So sorry if it offended anyone. :-( I apologize.

Thank you all for your replies. I should have read the FAQ, sorry.

Hrvoje Bratanic AKA Shuckey AKA Crtowat AKA No-Fun
 
B

BobR

Shuckey said:
What hath BobR wrought:
It's a song lyric. Just sing along.
[ "Take a walk on the wild side", I think. (?) ]

Yes. So sorry if it offended anyone. :-( I apologize.

No need to apologize. As long as you don't post threats, cuss words, etc.,
there's no problem. (but do keep the subject matter On-Topic. You don't want
to anger the ones who could help you (don't burn bridges)).

A short lyric from a classic song in a sig is no problem.

BTW, I couldn't remember who did that song. Do you know?
 
J

James Kanze

Shuckey said:
Is it possible to define a sterile class in C++? That is, a class that
cannot be inherited from?

Sure: just don't document how to use the class as a base class.
No competent programmer will derive from a class without
understanding the consequences, and what it means, and if you
don't document them, he won't know about them.
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top