Don't want to make my class as base class

Discussion in 'C++' started by Nataraj M, Jul 29, 2005.

  1. Nataraj M

    Nataraj M Guest

    Hi, I just don't want anybody derive a class from my class. For
    example:

    ///////////////////////
    //MY CODE

    class MyClass {
    ....
    };
    ///////////////////////


    /////////////////////
    // Somebody's code

    class HisClass : public MyClass { // This line should give a compile
    time error
    ....
    }
    ////////////////////

    How can I make MyClass not to be a base class of other

    Thanks,
    Nataraj
    Nataraj M, Jul 29, 2005
    #1
    1. Advertising

  2. * Nataraj M:
    > Hi, I just don't want anybody derive a class from my class. For
    > example:
    >
    > ///////////////////////
    > //MY CODE
    >
    > class MyClass {
    > ...
    > };
    > ///////////////////////
    >
    >
    > /////////////////////
    > // Somebody's code
    >
    > class HisClass : public MyClass { // This line should give a compile
    > time error
    > ...
    > }
    > ////////////////////
    >
    > How can I make MyClass not to be a base class of other


    The FAQ is at <url: http://www.parashift.com/c++-faq-lite/>.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 29, 2005
    #2
    1. Advertising

  3. Nataraj M

    Frank Chang Guest

    Alf Steinbach: Here's an excerpt from www.artimus.com which may help
    you.

    Nguyen Minh Huy

    How to create final class in C++? Posted: Jun 18, 2005 6:02 AM


    Re: How to create final class in C++? Posted: Jun 25, 2005 12:51 AM
    Reply

    responding to Nguyen

    In C++ there is no keyword (final) to declare a class as
    non-inheritable as in Java. But then C++ has its own features which you
    may exploit to get the same behaviour.

    Bsically it uses concepts of private constructor and friend class.

    Idea is if you derive(virtual public) a class from another class having
    private constructors, you cannot create an object of derived class.

    To avoid this you need to declare derived (Final class) class as friend
    of base class. So that now if some one tries to inherit from this Final
    class, compilation gives error as this class cannot call constructor of
    its super class i.e. Final class's super class i.e. base class that has
    private constructor.

    Hope this helps.

    regards,
    Shashank





    Alf P. Steinbach wrote:
    > * Nataraj M:
    > > Hi, I just don't want anybody derive a class from my class. For
    > > example:
    > >
    > > ///////////////////////
    > > //MY CODE
    > >
    > > class MyClass {
    > > ...
    > > };
    > > ///////////////////////
    > >
    > >
    > > /////////////////////
    > > // Somebody's code
    > >
    > > class HisClass : public MyClass { // This line should give a compile
    > > time error
    > > ...
    > > }
    > > ////////////////////
    > >
    > > How can I make MyClass not to be a base class of other

    >
    > The FAQ is at <url: http://www.parashift.com/c++-faq-lite/>.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
    Frank Chang, Jul 29, 2005
    #3
  4. Frank Chang wrote:
    >
    > Alf Steinbach: Here's an excerpt from www.artimus.com which may help
    > you.
    >


    I don't think that Alf needs that help.

    > Bsically it uses concepts of private constructor and friend class.


    And this differs exactly in which way from what is described in the FAQ?

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jul 29, 2005
    #4
  5. Nataraj M

    Frank Chang Guest

    Karl, This post was intended for Nataraj M not Alf Steinbach. If one
    goes to the link you are referring to, then one has to do a search to
    find this topic. I thought I would save Nataraj M the time to do this
    search. Anyway, I think we are quibbling about something that has
    nothing to do with C++. Thank you for your moderator comments.

    Karl Heinz Buchegger wrote:
    > Frank Chang wrote:
    > >
    > > Alf Steinbach: Here's an excerpt from www.artimus.com which may help
    > > you.
    > >

    >
    > I don't think that Alf needs that help.
    >
    > > Bsically it uses concepts of private constructor and friend class.

    >
    > And this differs exactly in which way from what is described in the FAQ?
    >
    > --
    > Karl Heinz Buchegger
    >
    Frank Chang, Jul 29, 2005
    #5
    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. Alf P. Steinbach
    Replies:
    6
    Views:
    544
    John Carson
    Sep 3, 2005
  2. Hicham Mouline
    Replies:
    2
    Views:
    1,605
    Juha Nieminen
    Jan 31, 2009
  3. Hicham Mouline
    Replies:
    1
    Views:
    592
    Victor Bazarov
    Apr 20, 2009
  4. jeantoe
    Replies:
    0
    Views:
    463
    jeantoe
    Oct 4, 2010
  5. Karan Rajput
    Replies:
    2
    Views:
    139
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page