C++ (Qt) void **vtbl = reinterpret_cast<void***>( this )[ 0 ]; while( *vtbl ) std::cout << *vtbl++ << std::endl;
#include <iostream>using namespace std;class AbstractClass{protected: virtual void AbstractFunc() = 0;public: AbstractClass();};AbstractClass::AbstractClass(){ AbstractFunc();}int main(){ return 0;}
In constructor 'AbstractClass::AbstractClass()':warning: abstract virtual 'virtual void AbstractClass::AbstractFunc()' called from constructorundefined reference to `AbstractClass::AbstractFunc()'undefined reference to `AbstractClass::AbstractFunc()'collect2: ld returned 1 exit status
C++ (Qt)#include <iostream> using namespace std; class AbstractClass{protected: virtual void AbstractFunc() = 0;public: AbstractClass();}; void AbstractClass::AbstractFunc() {} AbstractClass::AbstractClass(){ AbstractFunc();} int main(){ return 0;}
#include <iostream>using namespace std;class AbstractClass{public: virtual void AbstractFunc() = 0;};template <class TChild> AbstractClass* AbstractClass_Create(AbstractClass *instance){ TChild* result = (TChild*)instance; result->AbstractFunc(); return result;}AbstractClass* AbstractClass_Create(AbstractClass *instance);class RealClass01: public AbstractClass{friend AbstractClass* AbstractClass_Create<RealClass01>(AbstractClass *instance);protected: void AbstractFunc() { cout << "RealClass01" << endl; } virtual void RealFunc() = 0;};template <class TChild> RealClass01* RealClass01_Create(RealClass01 *instance){ TChild* result = (TChild*)AbstractClass_Create<RealClass01>(instance); result->RealFunc(); return result;}RealClass01* RealClass01_Create(RealClass01 *instance);class FactClass01: public RealClass01{friend RealClass01* RealClass01_Create<FactClass01>(RealClass01 *instance);protected: void RealFunc() { cout << "FactClass01" << endl; }};class RealClass02: public AbstractClass{friend AbstractClass* AbstractClass_Create<RealClass02>(AbstractClass *instance);protected: void AbstractFunc() { cout << "RealClass02" << endl; }};int main(){ AbstractClass *fc_01 = RealClass01_Create<FactClass01>(new FactClass01); AbstractClass *rc_02 = AbstractClass_Create<RealClass02>(new RealClass02); delete fc_01; delete rc_02; return 0;}
RealClass01FactClass01RealClass02
AbstractClass *fc_01 = RealClass01_Create<FactClass01>(new FactClass01); AbstractClass *rc_02 = AbstractClass_Create<RealClass02>(new RealClass02);
C++ (Qt) AbstractClass *fc_01 = new FactClass01; fc_01->AbstractFunc(); AbstractClass *rc_02 = new RealClass02; fc_02->AbstractFunc();
AbstractClass *rc_01 = AbstractClass_Create<RealClass01>(new RealClass01);AbstractClass *rc_02 = AbstractClass_Create<RealClass02>(new RealClass02);
{ AbstractDialog *pDialog = createDialog("testName"); if(pDialog->initFunc()) //это виртуальный метод. pDialog->exec(); //в конструкторе флаг DeleteOnClose else delete pDialog;}