我写了一个没有虚函数的基类和派生类。 虚拟的使用通常是我在何时使用虚拟析构函数时所看到的指南。
但是,虽然我的类没有虚函数,但是当我传递它时,我以多态方式使用这些类。 因此,类Base应该实现一个虚拟析构函数?
class Base; class Derived; main() { base& baseVar = derived(); foo(baseVar); }I wrote a base and derived class that had no virtual functions. The use of virtual is typically the guide I see for when to use a virtual destructor.
However, while my classes have no virtual functions I am using the classes in a polymorphic way when I pass them around. Therefore, class Base should implement a virtual destructor?
class Base; class Derived; main() { base& baseVar = derived(); foo(baseVar); }最满意答案
没有多态性,因为您使用引用调用(将调用)非虚函数。 在您的示例中,您只需调用(将调用)基类的函数。
而且这句话
base& baseVar = derived();不应该编译因为您使用非const引用绑定临时对象。
必须有
const base& baseVar = derived();至于你的例子中的析构函数,则不需要虚拟析构函数。 因此,您定义了对派生类的临时对象的引用。 在这种情况下,临时对象将被称为自己的析构函数。 首先,将调用基类的析构函数(从派生类的析构函数中),然后执行派生类的析构函数体。
如果要在堆中分配派生内存并将分配的内存的地址分配给基类的指针,则需要使用vertual析构函数。 当你为这个指针调用operator delete时,如果你没有虚析构函数,那么就会调用基类的唯一析构函数。
例如
class Base; class Derived; main() { base* baseVar = new derived(); delete baseVar; // base shall have a virtual destructor }There is no polymorphism because you call (will call) non-virtual functions using the referemce. That is in your example you simply call (will call) functions of the base class.
Moreover this statement
base& baseVar = derived();should not be compiled because you bind a temporary object with a non-const reference.
There must be
const base& baseVar = derived();As for the destructor in your example then there is no need to have a virtual destructor. Becasue you defined a reference to a temporary object of the derived class. In this case for the temporary object will be called its own destructor. That is at first the destructor of the base class will be called (from the destructor of the derived class) and then the body of the destructor of the derived class will be executed.
The vertual destructor would be need if you would allocate a derived memory in the heap and assign the address of the allocated memory to a pointer of the base class. And when you would call operator delete for this pointer then if you have no virtual destructor then the only destructor of the base class would be called.
For example
class Base; class Derived; main() { base* baseVar = new derived(); delete baseVar; // base shall have a virtual destructor }没有虚函数,但还需要虚拟析构函数吗?(No virtual functions, but still need virtual destructor?)我写了一个没有虚函数的基类和派生类。 虚拟的使用通常是我在何时使用虚拟析构函数时所看到的指南。
但是,虽然我的类没有虚函数,但是当我传递它时,我以多态方式使用这些类。 因此,类Base应该实现一个虚拟析构函数?
class Base; class Derived; main() { base& baseVar = derived(); foo(baseVar); }I wrote a base and derived class that had no virtual functions. The use of virtual is typically the guide I see for when to use a virtual destructor.
However, while my classes have no virtual functions I am using the classes in a polymorphic way when I pass them around. Therefore, class Base should implement a virtual destructor?
class Base; class Derived; main() { base& baseVar = derived(); foo(baseVar); }最满意答案
没有多态性,因为您使用引用调用(将调用)非虚函数。 在您的示例中,您只需调用(将调用)基类的函数。
而且这句话
base& baseVar = derived();不应该编译因为您使用非const引用绑定临时对象。
必须有
const base& baseVar = derived();至于你的例子中的析构函数,则不需要虚拟析构函数。 因此,您定义了对派生类的临时对象的引用。 在这种情况下,临时对象将被称为自己的析构函数。 首先,将调用基类的析构函数(从派生类的析构函数中),然后执行派生类的析构函数体。
如果要在堆中分配派生内存并将分配的内存的地址分配给基类的指针,则需要使用vertual析构函数。 当你为这个指针调用operator delete时,如果你没有虚析构函数,那么就会调用基类的唯一析构函数。
例如
class Base; class Derived; main() { base* baseVar = new derived(); delete baseVar; // base shall have a virtual destructor }There is no polymorphism because you call (will call) non-virtual functions using the referemce. That is in your example you simply call (will call) functions of the base class.
Moreover this statement
base& baseVar = derived();should not be compiled because you bind a temporary object with a non-const reference.
There must be
const base& baseVar = derived();As for the destructor in your example then there is no need to have a virtual destructor. Becasue you defined a reference to a temporary object of the derived class. In this case for the temporary object will be called its own destructor. That is at first the destructor of the base class will be called (from the destructor of the derived class) and then the body of the destructor of the derived class will be executed.
The vertual destructor would be need if you would allocate a derived memory in the heap and assign the address of the allocated memory to a pointer of the base class. And when you would call operator delete for this pointer then if you have no virtual destructor then the only destructor of the base class would be called.
For example
class Base; class Derived; main() { base* baseVar = new derived(); delete baseVar; // base shall have a virtual destructor }
发布评论