Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A sterile class?

Reply
Thread Tools

A sterile class?

 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-09-2007
Shuckey wrote:
> 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.


 
Reply With Quote
 
 
 
 
Shuckey
Guest
Posts: n/a
 
      08-09-2007
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

--
And the colored girls go
doo do doo do doo do do doo.
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-09-2007
Shuckey wrote:
> 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.

 
Reply With Quote
 
Chris Thomasson
Guest
Posts: n/a
 
      08-09-2007
"Shuckey" <(E-Mail Removed)> wrote in message
news:s6tnb8h39ayd.6yd0e0yfckot$(E-Mail Removed)...
[...]

> --
> And the colored girls go
> doo do doo do doo do do doo.


What the heck does that mean?

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-09-2007
Gianni Mariani wrote:

> Shuckey wrote:
>> 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.


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....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.

}



Best

Kai-Uwe Bux
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      08-09-2007
Gianni Mariani wrote:
> Shuckey wrote:
>> 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


That should be:

class Sterile : virtual 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.
>

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      08-09-2007
Chris Thomasson wrote:
> "Shuckey" <(E-Mail Removed)> wrote in message
> news:s6tnb8h39ayd.6yd0e0yfckot$(E-Mail Removed)...
> [...]
>
>> --
>> And the colored girls go
>> doo do doo do doo do do doo.

>
> What the heck does that mean?


Let me tell you about Ahab.
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      08-09-2007

Chris Thomasson <(E-Mail Removed)> wrote in message...
> "Shuckey" <(E-Mail Removed)> wrote in message...
> [...]
> > - -
> > And the colored girls go
> > doo do doo do doo do do doo.

>
> What the heck does that mean?


It's a song lyric. Just sing along.

[ "Take a walk on the wild side", I think. (?) ]

--
Bob R
POVrookie


 
Reply With Quote
 
Shuckey
Guest
Posts: n/a
 
      08-10-2007
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
 
Reply With Quote
 
Shadowman
Guest
Posts: n/a
 
      08-10-2007
Chris Thomasson wrote:
> "Shuckey" <(E-Mail Removed)> wrote in message
> news:s6tnb8h39ayd.6yd0e0yfckot$(E-Mail Removed)...
> [...]
>
>> --
>> And the colored girls go
>> doo do doo do doo do do doo.

>
> What the heck does that mean?


Lou Reed.

Some lyrics just don't translate well on paper, it seems.

--
SM
rot13 for email
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New Henry's Outlet store very "sterile" RichA Digital Photography 12 04-03-2010 03:45 PM
[ETYMOLOGY] - Sterile Classes / Sterile Meta Classes Ilias Lazaridis Ruby 68 05-11-2005 10:24 AM
LCD monitors: crisp but sterile looking J. Tyler Digital Photography 75 12-13-2003 05:31 PM



Advertisments