嗷嗷按,今天被问到在constructor/destructor中调用virtual member function的问题。答错了,很羞耻。 依稀记得在constructor/destructor调用virtual member function不好,就随口答道不能调用,可能会出错。 后来仔细想想不对,羞耻呀。调用是可以的,从语言角度没错,只不过和其他情况下调用虚函数有些不同。 看代码: classA ...
3也就是说在base class构造期间,virtual函数不是virtual函数 同理对析构函数。
I want to call a virtual function that handles a member within a derivative class constructor, a destructor. Even if I pay attention to the initialization order (by initializing members after the virtual function call is complete), is there a possibility that an undefined action will occur becau...
There are different ways to approach this problem. One is to turn logTransaction into a non-virtual function in Transaction, then require that derived class constructors pass the necessary log information to the Transaction constructor. That function can then safely call the nonvirtual logTransaction....
that calls virtual function Component::set_owner_object(). There is only one implementation of this function, and it is in Component itself, and we dodged one bullet. Here's the implementation: inline Component::~Component() { set_owner_object(NULL); } Component::set_owner_object() is ...
You shouldn't call virtual functions from inside a constructor. The vtable isn't fully fixed up until the construction of the instance is complete.Try reworking things so you use 2-phase construction.12345678910111213141516 Window::Window(...) : hwnd(nullptr), ... { } bool Window::Init() ...
But howdoyou ensure that the proper version oflogTransactionis called each time an object in theTransactionhierarchy is created? Clearly, calling a virtual function on the object from theTransactionconstructor(s) is the wrong way to do it. ...
Reports virtual member function calls from constructors or destructors. Since construction starts with the base class and moves to the derived classes, the resources of the derived class are not yet initialized. Destruction is performed in reverse order, so calling a virtual function can lead to ...
It is often seen, that a virtual call in the constructor is meant to allow the derived type to set up some aspect of the base type. Such virtual method, in fact, should be a pure function that returns some value and doesn't depend on the state of the derived type. If this is the...
Thanks for looking it up, yeah - no virtual calls are being made in the destructor or constructor of the base class (UdpInterface, which LwipUdpInterface and MockUdpInterface inherit from). Seems like since the tests are being reported as passing, gtest got at least that far and the proble...