deriving a class...

Discussion in 'C++' started by Shraddha, Jun 19, 2007.

  1. Shraddha

    Shraddha Guest

    Can I stop people by deriving my class?
    I mean I don't want my class to be as a base class...
    Can I do that?
     
    Shraddha, Jun 19, 2007
    #1
    1. Advertising

  2. Shraddha wrote:
    > Can I stop people by deriving my class?
    > I mean I don't want my class to be as a base class...
    > Can I do that?


    Yes. See the FAQ.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 19, 2007
    #2
    1. Advertising

  3. Shraddha

    Sarath Guest

    On Jun 19, 11:10 am, Shraddha <> wrote:
    > Can I stop people by deriving my class?
    > I mean I don't want my class to be as a base class...
    > Can I do that?


    Yes you can but you have to do some tweaks with your class's
    construction and with inheritance.

    class Usable;

    class Usable_lock {
    friend class Usable;
    private:
    Usable_lock() {}
    Usable_lock(const Usable_lock&) {}
    };

    class Usable : public virtual Usable_lock {
    // ...
    public:
    Usable();
    Usable(char*);
    // ...
    };

    Usable a;

    class DD : public Usable { };

    DD dd; // error: DD::DD() cannot access
    // Usable_lock::Usable_lock(): private member

    You can check more about it at
    http://www.research.att.com/~bs/bs_faq2.html#no-derivation

    Regards,
    Sarath
    http://sarathc.wordpress.com/
     
    Sarath, Jun 19, 2007
    #3
  4. Shraddha

    Jim Langston Guest

    Jim Langston, Jun 19, 2007
    #4
  5. Shraddha

    Kai-Uwe Bux Guest

    Shraddha wrote:

    > Can I stop people by deriving my class?
    > I mean I don't want my class to be as a base class...
    > Can I do that?


    Technically, yes. See:

    http://www.research.att.com/~bs/bs_faq2.html#no-derivation

    A slightly different approach uses a protected constructor:

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

    }


    _However_ (!!!), as explained in Stroustrups FAQ, it is highly doubtfull
    that you have a valid reason to do something nasty like this. Keep in mind
    that you are probably unable to predict the reasons that clients of your
    code have to derive from your classes. Note that C++ supports multiple
    programming styles, where different reasons for derivation are considered
    valid. I doubt that you have sufficient information to decide for the
    client code programmers that derivation is wrong. That is their
    responsibility. Trust them; and if they screw up, it's their problem. That
    said, there might be cases where you have a valid reason, although I don't
    know of any convincing examples.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 19, 2007
    #5
  6. Shraddha

    James Kanze Guest

    On Jun 19, 4:10 am, Shraddha <> wrote:
    > Can I stop people by deriving my class?
    > I mean I don't want my class to be as a base class...
    > Can I do that?


    Yes and no. Unless you're in a very strange situation, just
    documenting the fact that the class isn't designed to be used as
    a base class should be enough. In fact, unless you're in a very
    strange situation, just not documenting how to use it as a base
    class should be sufficient.

    If it's not, you've got a serious problem, no matter what you
    do.

    --
    James Kanze (GABI Software, from CAI) 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, Jun 19, 2007
    #6
  7. On Tue, 19 Jun 2007 09:06:07 -0000, James Kanze wrote:

    >On Jun 19, 4:10 am, Shraddha <> wrote:
    >> Can I stop people by deriving my class?
    >> I mean I don't want my class to be as a base class...
    >> Can I do that?

    >
    >Yes and no. Unless you're in a very strange situation, just
    >documenting the fact that the class isn't designed to be used as
    >a base class should be enough. In fact, unless you're in a very
    >strange situation, just not documenting how to use it as a base
    >class should be sufficient.
    >
    >If it's not, you've got a serious problem, no matter what you
    >do.


    BTW, to clarify a too common misunderstanding, one may render any
    derived class non-instantiable, not preventing derivation itself (or
    usage of accessible static members in the derived class).

    class final
    {
    protected:
    final();
    };

    #define FINAL_CLASS private virtual final

    // usage:
    class my_class : FINAL_CLASS
    {};

    Why one might want to do that is another story :)

    --
    Gennaro Prota -- Need C++ expertise? I'm available
    https://sourceforge.net/projects/breeze/
    (replace 'address' with 'name.surname' to mail)
     
    Gennaro Prota, Jun 19, 2007
    #7
  8. Shraddha

    JohnQ Guest

    "James Kanze" <> wrote in message
    news:...
    On Jun 19, 4:10 am, Shraddha <> wrote:
    > Can I stop people by deriving my class?
    > I mean I don't want my class to be as a base class...
    > Can I do that?


    "Yes and no. Unless you're in a very strange situation, just
    documenting the fact that the class isn't designed to be used as
    a base class should be enough. "

    A non-virtual destructor would be telltale "documentation".

    John
     
    JohnQ, Jun 22, 2007
    #8
  9. Shraddha

    James Kanze Guest

    On Jun 22, 7:57 am, "JohnQ" <>
    wrote:
    > "James Kanze" <> wrote in message


    > news:...
    > On Jun 19, 4:10 am, Shraddha <> wrote:


    > > Can I stop people by deriving my class?
    > > I mean I don't want my class to be as a base class...
    > > Can I do that?


    > "Yes and no. Unless you're in a very strange situation, just
    > documenting the fact that the class isn't designed to be used as
    > a base class should be enough. "


    > A non-virtual destructor would be telltale "documentation".


    Not always. What about std::exception?

    In fact, most of the time, I think the opposite applies. Unless
    the class is documented explicitly to be a base class, you
    shouldn't derive from it.

    --
    James Kanze (GABI Software, from CAI) 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, Jun 22, 2007
    #9
  10. Shraddha

    JohnQ Guest

    "James Kanze" <> wrote in message
    news:...
    On Jun 22, 7:57 am, "JohnQ" <>
    wrote:
    > "James Kanze" <> wrote in message


    > news:...
    > On Jun 19, 4:10 am, Shraddha <> wrote:


    > > Can I stop people by deriving my class?
    > > I mean I don't want my class to be as a base class...
    > > Can I do that?


    > "Yes and no. Unless you're in a very strange situation, just
    > documenting the fact that the class isn't designed to be used as
    > a base class should be enough. "


    > A non-virtual destructor would be telltale "documentation".


    "Not always. What about std::exception?"

    It's destructor is declared virtual in my implementation.

    "In fact, most of the time, I think the opposite applies. Unless
    the class is documented explicitly to be a base class, you
    shouldn't derive from it."

    John
     
    JohnQ, Jun 22, 2007
    #10
  11. Shraddha

    Kai-Uwe Bux Guest

    JohnQ wrote:

    >
    > "James Kanze" <> wrote in message
    > news:...
    > On Jun 22, 7:57 am, "JohnQ" <>
    > wrote:
    >> "James Kanze" <> wrote in message

    >
    >> news:...
    >> On Jun 19, 4:10 am, Shraddha <> wrote:

    >
    >> > Can I stop people by deriving my class?
    >> > I mean I don't want my class to be as a base class...
    >> > Can I do that?

    >
    >> "Yes and no. Unless you're in a very strange situation, just
    >> documenting the fact that the class isn't designed to be used as
    >> a base class should be enough. "

    >
    >> A non-virtual destructor would be telltale "documentation".

    >
    > "Not always. What about std::exception?"
    >
    > It's destructor is declared virtual in my implementation.


    Nonetheless, the standard library has some examples of template classes
    designed for derivation that do just fine without virtual destructors.
    Think of unary_function<>, binary_function<>, or iterator<>.

    Derivation in C++ is a concept orthogonal to the various programming styles
    supported by the language. In the context of policy based design,
    derivation from classes is often done without the need for virtual
    destructors. The slogan that classes to be derived from need virtual
    destructors is firmly rooted in the OO style and of somewhat limited scope.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 22, 2007
    #11
  12. Shraddha

    James Kanze Guest

    On Jun 22, 7:33 pm, "JohnQ" <>
    wrote:
    > "James Kanze" <> wrote in message
    > news:...
    > On Jun 22, 7:57 am, "JohnQ" <>
    > wrote:


    > > "James Kanze" <> wrote in message
    > >news:...
    > > On Jun 19, 4:10 am, Shraddha <> wrote:
    > > > Can I stop people by deriving my class?
    > > > I mean I don't want my class to be as a base class...
    > > > Can I do that?

    > > "Yes and no. Unless you're in a very strange situation, just
    > > documenting the fact that the class isn't designed to be used as
    > > a base class should be enough. "
    > > A non-virtual destructor would be telltale "documentation".


    > "Not always. What about std::exception?"


    > It's destructor is declared virtual in my implementation.


    Oops. I meant std::iterator.

    --
    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, Jun 22, 2007
    #12
  13. On Jun 19, 4:06 am, James Kanze <> wrote:
    > Yes and no. Unless you're in a very strange situation, just
    > documenting the fact that the class isn't designed to be used as
    > a base class should be enough.


    By the same logic, just documenting that a person should not call a
    particular method under any circumstances should be enough. Therefore
    the "private" and "protected" keywords can safely be removed from the
    language.

    Declaring the destructor non-virtual is, unfortunately, the best
    option we have to ensure people don't derive from classes unless you
    want to seriously uglify your code. A keyword similar to
    "final" (Java) or "sealed" (C#) would definitely be nice though, and
    would probably see considerable use.
     
    Zachary Turner, Jun 22, 2007
    #13
    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. Mahesh Devjibhai Dhola

    Problem in deriving custome class from XmlNode

    Mahesh Devjibhai Dhola, Oct 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    518
    Mahesh Devjibhai Dhola
    Oct 15, 2004
  2. Gary Rynearson

    Problem deriving from WebControl Class

    Gary Rynearson, Nov 18, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    432
    Gary Rynearson
    Nov 18, 2005
  3. Matthias Kaeppler
    Replies:
    1
    Views:
    471
    R.F. Pels
    May 22, 2005
  4. Steven T. Hatton

    Deriving a regular class from a template?

    Steven T. Hatton, Apr 23, 2004, in forum: C++
    Replies:
    28
    Views:
    2,224
    Richard Herring
    Apr 28, 2004
  5. markww
    Replies:
    1
    Views:
    303
    Ivan Vecerina
    Aug 22, 2006
Loading...

Share This Page