3

I read that that virtual destructors must be declared in classes that have virtual methods. I just cant understand why they must be declared virtual. I know why we need to have virtual destructors as from the following example. I just wanted to know why compilers dont manage virtual destructors for us. Is there something I need to know about working of virtual destructors ? The following example shows that if destructors are not declared virtual the destructors of derived class are not called why is that ?

class Base 
{
    // some virtual methods
public:
    Base()
    {std::cout << "Base Constructor\n";}
    ~Base()
    {std::cout << "Base De-structor\n";}

};

class Derived : public Base
{
public:
    Derived()
    {std::cout << "Der constructor\n";}
    ~Derived()
    { std::cout << "Der De-structor\n";}
} ;         
void main()
{

    Base *b = new Derived();
    delete b;
}
4

3 に答える 3

4

I just wanted to know why compilers dont manage virtual destructors for us.

Because in C++, you pay for what you use. Having a virtual destructor by default involves the compiler adding a virtual table pointer to the class, which increases its size. This is not always desirable.

The following example shows that if destructors are not declared virtual the destructors of derived class are not called why is that ?

The example exibits undefined behavior. It's simply against the rules. The fact that not all destructors are called is just one possible manifestation. It could possibly crash.

Is there something I need to know about working of virtual destructors ?

Yes. They are required if you're deleting an object through a pointer to a base class. Otherwise it's undefined behavior.

5.3.5 Delete [expr.delete]

3) In the first alternative (delete object), if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined. In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined. (emphasis mine)

于 2012-10-22T20:21:19.433 に答える
3

仮想デストラクタは、仮想メソッドを持つクラスで宣言する必要があることを読みました。

「しなければならない」という言葉は強すぎます。「しなければならない」という言葉は、そのアドバイスにはるかによく当てはまります。

コンパイラが仮想デストラクタを管理しない理由を知りたかっただけです。

C ++の設計者は、最も極端な状況でのみコンパイラーに要求されていないことをコンパイラーが実行することを避けようとしました。言語設計者は、クラスをポリモーフィックにする決定はプログラムの設計者に委ねられるべきであることを認識していたため、この責任をコンパイラに再割り当てすることを拒否しました。

次の例は、デストラクタが仮想として宣言されていない場合、派生クラスのデストラクタが呼び出されないことを示しています。それはなぜですか。

コードが無効であるため:Derived非仮想のデストラクタを宣言することにより、 ;Derivedへのポインタを介して決して破壊しないことを約束しました。Baseあなたmainはこの約束を破り、未定義の振る舞いを引き起こします。

正確な型で変数を宣言するだけbで、非仮想デストラクタ(ideoneへのリンク)に関連する問題を回避できることに注意してください。ただし、これはかなり不安定な設計につながるため、仮想関数と非仮想デストラクタを使用した継承階層は避ける必要があります。

于 2012-10-22T20:29:26.000 に答える
3

I read that that virtual destructors must be declared in classes that have virtual methods.

Yes. But that is an oversimplification.
Its not that a class with virtual methods needs a virtual destructor. But the way a class with virtual methods is used means that it will usually need a virtual destructor. A virtual destructor is ONLY needed if you delete an object via a pointer to its base class. The problem is that when an object has virtually methods you are usually working with a pointer to its base class even though the actual object is slightly different.

I just cant understand why they must be declared virtual.

It's not that they must. As explained above. This is a result of the usual usage patterns.

I just wanted to know why compilers dont manage virtual destructors for us.

Because it is not always needed. And the ethos of C++ is you don't have to pay for something you don't need it. If the compiler always added virtual destructors to a class with virtual methods then I would have to pay the price of using a virtual destructor even in situations I can prove in my code base that I don't need it.

Is there something I need to know about working of virtual destructors ?

Just that there is a slight cost to using them.

if destructors are not declared virtual the destructors of derived class are not called why is that ?

That is why we have virtual destructors to cause this behavior. If you need this behavior you need to add virtual destructors. But there are cases were virtual destructors may not be required which allows the user of this method not to pay the price.

于 2012-10-22T20:32:17.650 に答える