A sterile class?

Discussion in 'C++' started by Victor Bazarov, Aug 9, 2007.

  1. 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.
     
    Victor Bazarov, Aug 9, 2007
    #1
    1. Advertising

  2. Victor Bazarov

    Shuckey Guest

    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.
     
    Shuckey, Aug 9, 2007
    #2
    1. Advertising

  3. 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.
     
    Gianni Mariani, Aug 9, 2007
    #3
  4. "Shuckey" <> wrote in message
    news:s6tnb8h39ayd.6yd0e0yfckot$...
    [...]

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


    What the heck does that mean?
     
    Chris Thomasson, Aug 9, 2007
    #4
  5. Victor Bazarov

    Kai-Uwe Bux Guest

    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...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
     
    Kai-Uwe Bux, Aug 9, 2007
    #5
  6. 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.
    >
     
    Gianni Mariani, Aug 10, 2007
    #6
  7. Chris Thomasson wrote:
    > "Shuckey" <> wrote in message
    > news:s6tnb8h39ayd.6yd0e0yfckot$...
    > [...]
    >
    >> --
    >> 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.
     
    Gianni Mariani, Aug 10, 2007
    #7
  8. Victor Bazarov

    BobR Guest

    Chris Thomasson <> wrote in message...
    > "Shuckey" <> 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
     
    BobR, Aug 10, 2007
    #8
  9. Victor Bazarov

    Shuckey Guest

    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
     
    Shuckey, Aug 10, 2007
    #9
  10. Victor Bazarov

    Shadowman Guest

    Chris Thomasson wrote:
    > "Shuckey" <> wrote in message
    > news:s6tnb8h39ayd.6yd0e0yfckot$...
    > [...]
    >
    >> --
    >> 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
     
    Shadowman, Aug 10, 2007
    #10
  11. Victor Bazarov

    BobR Guest

    Shuckey <> wrote in message...
    > 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?
    --
    Bob R
    POVrookie
     
    BobR, Aug 10, 2007
    #11
  12. Victor Bazarov

    James Kanze Guest

    Shuckey wrote:

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

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Aug 11, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cyril
    Replies:
    2
    Views:
    3,927
    cyril
    Aug 25, 2004
  2. E11
    Replies:
    1
    Views:
    4,902
    Thomas Weidenfeller
    Oct 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    775
    Pete Becker
    May 4, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    618
  5. Ilias Lazaridis
    Replies:
    68
    Views:
    654
    David A. Black
    May 11, 2005
Loading...

Share This Page