protected inheritance modifier

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wenjie

    protected inheritance modifier

    Hello!


    Suppose I have:

    class B {
    protected:
    virtual ~B();
    };

    class D: public B {
    public:
    ~D();
    };


    Is it a good practice to use "protected" in class B?
  • Gianni Mariani

    #2
    Re: protected inheritance modifier

    Wenjie wrote:[color=blue]
    > Hello!
    >
    >
    > Suppose I have:
    >
    > class B {
    > protected:
    > virtual ~B();
    > };
    >
    > class D: public B {
    > public:
    > ~D();
    > };
    >
    >
    > Is it a good practice to use "protected" in class B?[/color]

    only if you want methods in classes that inherit B be the only methods
    able to delete B.

    Comment

    • stephan beal

      #3
      Re: protected inheritance modifier

      Wenjie wrote:[color=blue]
      > Suppose I have:
      >
      > class B {
      > protected:
      > virtual ~B();
      > };
      >
      > class D: public B {
      > public:
      > ~D();
      > };
      >
      >
      > Is it a good practice to use "protected" in class B?[/color]

      Not an answer to your question, but something to keep i mind:
      Making your dtor protected will keep your class from being used in 3rd-party
      custom memory-mgt code which calls the dtor directly (as in,
      thefoo->~foo()).

      --
      ----- stephan beal
      Registered Linux User #71917 http://counter.li.org
      I speak for myself, not my employer. Contents may
      be hot. Slippery when wet. Reading disclaimers makes
      you go blind. Writing them is worse. You have been Warned.

      Comment

      • Victor Bazarov

        #4
        Re: protected inheritance modifier

        "stephan beal" <stephan@wander inghorse.net> wrote...[color=blue]
        > Wenjie wrote:[color=green]
        > > Suppose I have:
        > >
        > > class B {
        > > protected:
        > > virtual ~B();
        > > };
        > >
        > > class D: public B {
        > > public:
        > > ~D();
        > > };
        > >
        > >
        > > Is it a good practice to use "protected" in class B?[/color]
        >
        > Not an answer to your question, but something to keep i mind:
        > Making your dtor protected will keep your class from being used in[/color]
        3rd-party[color=blue]
        > custom memory-mgt code which calls the dtor directly (as in,
        > thefoo->~foo()).[/color]

        .... or indirectly, anywhere, as in

        {
        foo afoo;
        ...
        } // afoo::~foo() is supposed to be called here

        Victor


        Comment

        Working...