C ++ abstract class destructor

Is it good practice (and is it possible) to create an abstract class using only a pure virtual destructor in the parent class?

Here is a sample

class AbstractBase {
public:
    AbstractBase () {}
    virtual ~AbstractBase () = 0;
};

class Derived : public AbstractBase {
public:
    Derived() {}
    virtual ~Derived() {}
};

Otherwise, how can I create an abstract class if the attributes and constructors of the derived class are all the same and the other method is completely different?

+4
source share
3 answers

, , , .
, RTTI , _ , , . .

, , , .
( "= 0"!)

struct AbstractBase {
     virtual ~AbstractBase() = 0;
}

AbstractBase::~AbstractBase() {
}

( , ).

+5

++ , . (, = 0 ). , , , . , :

class ISampleInterface
{
public:
    virtual ~ISampleInterface() { };
    virtual void Method1() = 0;
    virtual void Method2() = 0;
}

class SampleClass : public ISampleInterface
{
public:
    SampleClass() { };
    void Method1() { };
    void Method2() { };
}

int main()
{
    ISampleInterface *pObject = (ISampleInterface*)new SampleClass();
    delete pObject;
    return 0;
}
+12

New Thing:

, - ... , , , .

... : " , , , ".

virtual. , . , , Foo, , , . , , .

, :

struct Foo {
    virtual ~Foo() = 0;
};

struct Bar : Foo {
    ~Bar() { }
};

struct Baz : Foo {
    ~Baz() override { } // C++11
};

, , , Bar::~Bar() Baz::~Baz() , Foo::~Foo() , Foo::~Foo() , Bar Baz .

. -, virtual :

struct Foo {
    virtual ~Foo() { }
};

struct Bar : Foo {
    ~Bar() { }
};

struct Baz : Foo {
    ~Baz() override { } // C++11
};

struct Quux : Foo { };

. , , : , :

struct Foo {
    virtual ~Foo() = 0;
};

Foo::~Foo() { }

struct Bar : Foo {
    ~Bar() { }
};

struct Baz : Foo {
    ~Baz() override { } // C++11
};

struct Quux : Foo { };

, , , . , . , .

, , , ?

, , - - Java-. , , , . , , , . .

+2

Source: https://habr.com/ru/post/1545296/


All Articles