Derive or not to

Discussion in 'C++' started by bobsled, Apr 28, 2004.

  1. bobsled

    bobsled Guest

    Generally, what should be done to make a class not expected to be derived
    from?

    What should be done to a class to make it derivable?

    In an abstract base class, the destructor is automatically "virtual?

    If the constructor of a class is protected or private, how to code the class
    so that it provides a public member function, or to declare a friend that
    has access to the protected or private constructor and thus the class
    becomes instantiable?

    Thanks for your comments!
    bobsled, Apr 28, 2004
    #1
    1. Advertising

  2. bobsled

    Sharad Kala Guest

    "bobsled" <> wrote in message
    news:wzIjc.30233$...
    > Generally, what should be done to make a class not expected to be derived
    > from?


    There is no final keyword like Java to stop derivation in C++.
    There is a way though. Read this -
    http://www.research.att.com/~bs/bs_faq2.html#no-derivation

    > What should be done to a class to make it derivable?


    By default it is derivable.

    > In an abstract base class, the destructor is automatically "virtual?


    No

    > If the constructor of a class is protected or private, how to code the class
    > so that it provides a public member function, or to declare a friend that
    > has access to the protected or private constructor and thus the class
    > becomes instantiable?


    You could do either way, depends on what you are trying to achieve. The public
    member function of the class has to be static though.

    -Sharad
    Sharad Kala, Apr 28, 2004
    #2
    1. Advertising

  3. bobsled

    tom_usenet Guest

    On Wed, 28 Apr 2004 06:55:24 GMT, "bobsled" <> wrote:

    >Generally, what should be done to make a class not expected to be derived
    >from?


    Don't give it any virtual functions and document the fact that it is a
    concrete class. It is possible to force non-derivation, but there
    generally isn't much point.

    >What should be done to a class to make it derivable?


    Give it at least one virtual function, and document how that should be
    overridden. You will almost always want a virtual destructor too.

    >In an abstract base class, the destructor is automatically "virtual?


    No, you have to explicitly declare it to be virtual.

    >If the constructor of a class is protected or private, how to code the class
    >so that it provides a public member function, or to declare a friend that
    >has access to the protected or private constructor and thus the class
    >becomes instantiable?


    friend class MyFriend;

    or

    public:
    static Foo* createFoo() {return new Foo();}

    unless I didn't understand the question?

    Tom
    --
    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    tom_usenet, Apr 28, 2004
    #3
    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. Amil
    Replies:
    1
    Views:
    358
    Teemu Keiski
    Nov 20, 2003
  2. Henke
    Replies:
    4
    Views:
    433
    Scott Allen
    Aug 13, 2004
  3. JC
    Replies:
    0
    Views:
    417
  4. Chad Austin
    Replies:
    0
    Views:
    267
    Chad Austin
    Aug 20, 2007
  5. Markus Dehmann
    Replies:
    2
    Views:
    627
    Abhishek Padmanabh
    Jun 8, 2008
Loading...

Share This Page