Abstract base class with no virtual methods?

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

    #1

    Abstract base class with no virtual methods?

    I want to have three classes; Parent, Child1, and Child2. Parent is to be
    an abstract base class with Child1 and Child2 concrete classes derived from
    Parent.

    Normally, to do that, Parent would have some virtual method defined as
    "=0", and implemented in the two children. The problem I have is that the
    only difference between the three classes is in the constructor. In fact,
    the two child classes won't define any methods at all other than their
    constructors.

    What's the best way to make it impossible to instantiate an instance of
    Parent in a situation like this?
  • Victor Bazarov

    #2
    Re: Abstract base class with no virtual methods?

    Roy Smith wrote:[color=blue]
    > I want to have three classes; Parent, Child1, and Child2. Parent is
    > to be an abstract base class with Child1 and Child2 concrete classes
    > derived from Parent.
    >
    > Normally, to do that, Parent would have some virtual method defined as
    > "=0", and implemented in the two children. The problem I have is
    > that the only difference between the three classes is in the
    > constructor. In fact, the two child classes won't define any methods
    > at all other than their constructors.
    >
    > What's the best way to make it impossible to instantiate an instance
    > of Parent in a situation like this?[/color]

    The simplest of course is to declare the destructor virtual (and you do
    need that if you intend to destroy 'Child1' or 'Child2' object through
    a pointer to 'Parent') and declare it pure, and then define it outside
    of the class (empty-bodied if it has nothing to do in 'Parent').

    V


    Comment

    • Andrey Tarasevich

      #3
      Re: Abstract base class with no virtual methods?

      Roy Smith wrote:[color=blue]
      > I want to have three classes; Parent, Child1, and Child2. Parent is to be
      > an abstract base class with Child1 and Child2 concrete classes derived from
      > Parent.
      >
      > Normally, to do that, Parent would have some virtual method defined as
      > "=0", and implemented in the two children. The problem I have is that the
      > only difference between the three classes is in the constructor. In fact,
      > the two child classes won't define any methods at all other than their
      > constructors.
      >
      > What's the best way to make it impossible to instantiate an instance of
      > Parent in a situation like this?[/color]

      If you don't need _any_ polymorphic behavior from your 'Parent' class (including
      polymorphic deletion), I'd suggest that you simply declare all 'Parent's
      constructors with 'protected' access specification.

      --
      Best regards,
      Andrey Tarasevich


      Comment

      • Roy Smith

        #4
        Re: Abstract base class with no virtual methods?

        In article <11najestinutmc 5@news.supernew s.com>,
        Andrey Tarasevich <andreytarasevi ch@hotmail.com> wrote:
        [color=blue]
        > Roy Smith wrote:[color=green]
        > > I want to have three classes; Parent, Child1, and Child2. Parent is to be
        > > an abstract base class with Child1 and Child2 concrete classes derived from
        > > Parent.
        > >
        > > Normally, to do that, Parent would have some virtual method defined as
        > > "=0", and implemented in the two children. The problem I have is that the
        > > only difference between the three classes is in the constructor. In fact,
        > > the two child classes won't define any methods at all other than their
        > > constructors.
        > >
        > > What's the best way to make it impossible to instantiate an instance of
        > > Parent in a situation like this?[/color]
        >
        > If you don't need _any_ polymorphic behavior from your 'Parent' class
        > (including
        > polymorphic deletion), I'd suggest that you simply declare all 'Parent's
        > constructors with 'protected' access specification.
        >
        > --
        > Best regards,
        > Andrey Tarasevich[/color]

        That sounds pretty straight-forward. Thanks.

        Comment

        • JefferyRen@gmail.com

          #5
          Re: Abstract base class with no virtual methods?


          Roy Smith wrote:[color=blue]
          > In article <11najestinutmc 5@news.supernew s.com>,
          > Andrey Tarasevich <andreytarasevi ch@hotmail.com> wrote:
          >[color=green]
          > > Roy Smith wrote:[color=darkred]
          > > > I want to have three classes; Parent, Child1, and Child2. Parent is to be
          > > > an abstract base class with Child1 and Child2 concrete classes derived from
          > > > Parent.
          > > >
          > > > Normally, to do that, Parent would have some virtual method defined as
          > > > "=0", and implemented in the two children. The problem I have is that the
          > > > only difference between the three classes is in the constructor. In fact,
          > > > the two child classes won't define any methods at all other than their
          > > > constructors.
          > > >
          > > > What's the best way to make it impossible to instantiate an instance of
          > > > Parent in a situation like this?[/color]
          > >
          > > If you don't need _any_ polymorphic behavior from your 'Parent' class
          > > (including
          > > polymorphic deletion), I'd suggest that you simply declare all 'Parent's
          > > constructors with 'protected' access specification.
          > >
          > > --
          > > Best regards,
          > > Andrey Tarasevich[/color]
          >
          > That sounds pretty straight-forward. Thanks.[/color]

          Comment

          • Duane Hebert

            #6
            Re: Abstract base class with no virtual methods?


            "Roy Smith" <roy@panix.co m> wrote in message
            news:roy-4AD839.20302111 112005@reader2. panix.com...[color=blue]
            >I want to have three classes; Parent, Child1, and Child2. Parent is to be
            > an abstract base class with Child1 and Child2 concrete classes derived
            > from
            > Parent.
            >
            > Normally, to do that, Parent would have some virtual method defined as
            > "=0", and implemented in the two children. The problem I have is that the
            > only difference between the three classes is in the constructor. In fact,
            > the two child classes won't define any methods at all other than their
            > constructors.
            >
            > What's the best way to make it impossible to instantiate an instance of
            > Parent in a situation like this?[/color]

            Make the dtor virtual. It needs to be virtual anyway if you want
            to use it polymorphically . Why do you need the base to be
            abstract? Are you just trying to prevent it being instantiated?
            If that's the case, you may make the dtor pure virtual but then
            I think you'll need to implement it in the derived classes instead of
            using a default one.


            Comment

            Working...