How to delete an object of a polymorphic class type that has no virtual destructor

后端 未结 6 1266
清酒与你
清酒与你 2021-02-07 09:37

I am getting the following error when I try to compile some code from a Third-party SDK.

*Description    Resource    Path    Location    Type
deleting object of          


        
6条回答
  •  天命终不由人
    2021-02-07 10:04

    You cannot safely use a pointer to the base class, unless it has a virtual destructor declared on the base class. Since this is a vendor library, you can not add the virtual destructor needed.

    If the library itself does not create subclasses of this object, you may be able to get the effect you need by declaring a subclass for this object and use that object as a base class.

    class NotGoodBase {
     ~NotGoodBase(); // Non-virtual destructor.  Possibly added by compiler.
    };
    
    class UseThisAsBase : public NotGoodBase {
     virtual ~UseThisAsBase(); // Virtual destructor.
    };
    

    You should be able to use pointers of type UseThisAsBase anywhere that you could use NotGoodBase, except places where you need to have an LValue of type NotGoodBase, such as an assignment or when passing a variable by reference.

提交回复
热议问题