How to prevent a class from being inheritable, in C++

Discussion in 'C++' started by Saumya, Feb 10, 2004.

  1. Saumya

    Saumya Guest

    Hi,

    I didn't know how else to express myself in the subject line, but what
    I want to know is this: <b>I have a class A, so what do I have to do
    to ensure that no one can derive from it?</b>

    Saumya
     
    Saumya, Feb 10, 2004
    #1
    1. Advertising

  2. Saumya wrote:

    > Hi,
    >
    > I didn't know how else to express myself in the subject line, but what
    > I want to know is this: <b>I have a class A, so what do I have to do
    > to ensure that no one can derive from it?</b>
    >
    > Saumya


    Why do you want to do this? Explaining what you want to achieve with this
    behaviour would help to find a meaningful solution.

    --
    To get my real email adress, remove the two onkas
    --
    Dipl.-Inform. Hendrik Belitz
    Central Institute of Electronics
    Research Center Juelich
     
    Hendrik Belitz, Feb 10, 2004
    #2
    1. Advertising

  3. As Christian already posted, there are at least three ways to do so. But: There's no way in C++ as simple as
    "final" in Java:
    The first approach needs additional static member functions, the second comment-only approach only
    works iff the user cooperates, and the third one needs a "friend" declaration in a virtual base class, which
    isn't nice either. And in my opinion "final"ization imposes a large limitation to the usage of you class.

    Therefore think twice, please, whether you can't change your design such not to need to make
    your class behave like "final" any more.

    Cheers,
    Philipp.

    "Saumya" <> wrote in message news:...
    > Hi,
    >
    > I didn't know how else to express myself in the subject line, but what
    > I want to know is this: <b>I have a class A, so what do I have to do
    > to ensure that no one can derive from it?</b>
    >
    > Saumya
     
    Philipp Bachmann, Feb 10, 2004
    #4
  4. "Philipp Bachmann
    >
    > As Christian already posted, there are at least three ways to do so. But: There's no way in C++ as simple as
    > "final" in Java:
    > The first approach needs additional static member functions, the second comment-only approach only
    > works iff the user cooperates, and the third one needs a "friend" declaration in a virtual base class, which
    > isn't nice either. And in my opinion "final"ization imposes a large limitation to the usage of you class.
    >


    Hmm. So it seems I am not alone.
    All always wondered: Why would one want to impose such a 'finalization' to a class. What
    is the purpose of doing so?

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Feb 10, 2004
    #5
  5. I think the only good reason is to force users of the class to aggregate
    instead of inherit.
    Consider a socket implementation, normally a socket class gives no meaning
    to inherit from, it is like inheriting from std::iostream..
    Sure there maybe where it could make sence, but for common use it doesn't..

    Jesper

    "Karl Heinz Buchegger" <> wrote in message
    news:...
    > "Philipp Bachmann
    > >
    > > As Christian already posted, there are at least three ways to do so.

    But: There's no way in C++ as simple as
    > > "final" in Java:
    > > The first approach needs additional static member functions, the second

    comment-only approach only
    > > works iff the user cooperates, and the third one needs a "friend"

    declaration in a virtual base class, which
    > > isn't nice either. And in my opinion "final"ization imposes a large

    limitation to the usage of you class.
    > >

    >
    > Hmm. So it seems I am not alone.
    > All always wondered: Why would one want to impose such a 'finalization' to

    a class. What
    > is the purpose of doing so?
    >
    > --
    > Karl Heinz Buchegger
    >
     
    Jesper Madsen, Feb 10, 2004
    #6
  6. Saumya

    Saumya Guest

    Not a very technical reason pal, but it just struck me while I was
    learning Java and encountered the "final" keyword there.

    Also, it struck me that what if I was asked this question in some
    technical interview ;-)
     
    Saumya, Feb 26, 2004
    #7
  7. Saumya <> spoke thus:

    > Not a very technical reason pal, but it just struck me while I was
    > learning Java and encountered the "final" keyword there.


    http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.8

    > Also, it struck me that what if I was asked this question in some
    > technical interview ;-)


    Just memorize the above URL? ;)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Feb 26, 2004
    #8
  8. I am really wondering who needs such a thing ...

    Razvan

    "Christopher Benson-Manica" <> schrieb im
    Newsbeitrag news:c1kq80$q3k$...
    > Saumya <> spoke thus:
    >
    > > Not a very technical reason pal, but it just struck me while I was
    > > learning Java and encountered the "final" keyword there.

    >
    > http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.8
    >
    > > Also, it struck me that what if I was asked this question in some
    > > technical interview ;-)

    >
    > Just memorize the above URL? ;)
    >
    > --
    > Christopher Benson-Manica | I *should* know what I'm talking about - if I
    > ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Razvan Popovici, Feb 26, 2004
    #9
  9. > Hmm. So it seems I am not alone.
    > All always wondered: Why would one want to impose such a 'finalization' to a class. What
    > is the purpose of doing so?


    No you aren't alone. 'finalization' seems almost equivalent to asking:

    "How can I prevent someone from reusing my code?"

    The idiom seems to fit well in Java, however. I always do feel like
    I'm wearing a straight-jacket when I code in Java. So heck, why not
    'finalize' the classes to prevent there reuse. After all, you never
    know when someone (possibly even yourself) might try to reuse it
    without your permission possibly messing up the aesthetic symmetry
    your 'pre-planned' class hierarchy. Shame on them.

    "I have opinions, strong opinions, but, I don't always agree with
    them."
    -- G. W. Bush
     
    Keith H Duggar, Mar 2, 2004
    #10
  10. Saumya

    Datta Patil Guest

    "Saumya" <> wrote in message
    news:...
    > Not a very technical reason pal, but it just struck me while I was
    > learning Java and encountered the "final" keyword there.
    >

    U mean how to simulate final java class concept in c++.....here is
    program.I guess its similar to creating singleton pattern

    // Make ctor private so that class cannot be inheriated

    //Use static member function to create object of its own which will return
    ptr to same class obj

    #include <iostream.h>

    class X{

    private :

    int x;

    X(); // private ctor

    public :

    static X* getMePrivateObject(); // this will help me creating handle rather
    for normal object

    };


    X::X(){

    cout<<"in constructor"<<endl;

    x=0;

    };


    X* X::getMePrivateObject(){

    X *pMyP = new X();

    return pMyP;

    };

    /*class Y : public X //cannot do this as X has private ctor

    {

    public:

    Y(){}

    void print(){ cout<<"Display me if u can";}

    };

    */

    void main(void){

    X *pMyP = X::getMePrivateObject();

    //Rest of ur code

    }
     
    Datta Patil, Mar 2, 2004
    #11
  11. Saumya

    Datta Patil Guest

    Check this program....Class X acts like final class in java.

    #include <iostream.h>
    class X{
    private :
    int x;
    X(); // private ctor
    public :
    static X* getMePrivateObject(); // this will help me creating handle rather
    for normal object
    };
    X::X(){
    cout<<"in constructor"<<endl;
    x=0;
    };
    X* X::getMePrivateObject(){
    X *pMyP = new X();
    return pMyP;
    };
    /*class Y : public X //cannot do this as X has private ctor
    {
    public:
    Y(){}
    void print(){ cout<<"Display me if u can";}
    };
    */
    void main(void){
    X *pMyP = X::getMePrivateObject();
    //Rest of ur code
    }
    Njoy,

    Datta Patil
     
    Datta Patil, Mar 2, 2004
    #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. Indraseena

    Making base class non-inheritable

    Indraseena, Jun 22, 2006, in forum: C++
    Replies:
    6
    Views:
    484
    Indraseena
    Jun 26, 2006
  2. kj
    Replies:
    2
    Views:
    250
  3. Alexander Walker
    Replies:
    4
    Views:
    134
    Steven Cheng[MSFT]
    Feb 27, 2006
  4. dreamcat four
    Replies:
    31
    Views:
    363
    ara.t.howard
    Oct 16, 2009
  5. ara.t.howard
    Replies:
    0
    Views:
    103
    ara.t.howard
    Oct 12, 2009
Loading...

Share This Page