abstract class - enforce cannot instantiate

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

    abstract class - enforce cannot instantiate

    If I have an generic interface, but _also_ have the implementation for
    it and want to use that common implementation in my derived classes,
    what method does that leave me to make pure virtual such that my
    generic interface class cannot be instantiated?

    Do you make the destructor virtual?
    Do you move constructors to private?

    // What do I make pure virtual??
    AbstractClass
    {
    public:

    AbstractClass() {}

    virtual AbstractClass() {}

    virtual void Foo()
    {
    std::cout << "Foo!" << std::endl;
    }
    };


    DerivedClass : public AbstractClass
    {
    public:

    DerivedClass() {}

    ~DerivedClass() {}

    void Bar()
    {
    std::cout << "Bar!" <<std::endl;
    }
    };


    I don't want anyone to be able to:
    AbstractClass * abstract = new AbstractClass() ;

    I do want people to be able to:
    AbstractClass * abstract = new DerivedClass();
    abstract->Foo();

    and expect:
    "Foo!"

  • Kevin Frey

    #2
    Re: abstract class - enforce cannot instantiate

    Separate the implementation from the interface declaration:

    class MyInterface
    {
    public:
    virtual void DoSomething( ) = 0;
    };

    class MyDefaultImplem entation : MyInterface
    {
    virtual void DoSomething( )
    {
    // I am doing something here
    }
    };

    class MyDerivedClass : MyDefaultImplem entation
    {
    };


    Comment

    • =?utf-8?Q?David_C=C3=B4me?=

      #3
      Re: abstract class - enforce cannot instantiate

      On Tue, 05 Aug 2008 22:45:48 +0200, Christopher <cpisz@austin.r r.com>
      wrote:
      If I have an generic interface, but _also_ have the implementation for
      it and want to use that common implementation in my derived classes,
      what method does that leave me to make pure virtual such that my
      generic interface class cannot be instantiated?
      >
      Do you make the destructor virtual?
      Do you move constructors to private?
      >
      // What do I make pure virtual??
      AbstractClass
      {
      public:
      >
      AbstractClass() {}
      >
      virtual AbstractClass() {}
      >
      virtual void Foo()
      {
      std::cout << "Foo!" << std::endl;
      }
      };
      >
      >
      DerivedClass : public AbstractClass
      {
      public:
      >
      DerivedClass() {}
      >
      ~DerivedClass() {}
      >
      void Bar()
      {
      std::cout << "Bar!" <<std::endl;
      }
      };
      >
      >
      I don't want anyone to be able to:
      AbstractClass * abstract = new AbstractClass() ;
      >
      I do want people to be able to:
      AbstractClass * abstract = new DerivedClass();
      abstract->Foo();
      >
      and expect:
      "Foo!"
      >

      class AbstractClass
      {
      public:

      AbstractClass() {}

      virtual ~AbstractClass( )=0;
      virtual void Foo()
      {
      std::cout << "Foo!" << std::endl;
      }
      };

      AbstractClass:: ~AbstractClass( )
      {
      std::cout<<"Des t ABC"<<std::endl ;
      }

      With this code, AbstractClass's destructor is virtual pure and well
      defined.
      Try to instanciate an obbject of type AbstractClass, and you will get an
      error.

      Comment

      Working...