Cclass I{public: virtual void create()=0;} class A:public I {public: A(){ create(); }; ~A(); virtual void create(){}; }; class B:public B{public: B(); ~B(); virtual void create(){int i = 1+1;};}; int main(int argc, char *argv[]){ B * b = new B();}
Cint main(int argc, char *argv[]){ B * b = new B(); b->create();}
C++ (Qt)#include <iostream> template<class Derived>class A { public: A(Derived* d){d->init();}}; class B : public A<B> { public: B():A<B>(this){} void init(){std::cout << "B" << std::endl;} };class C : public A<C> { public: C():A<C>(this){} void init(){std::cout << "C" << std::endl;} }; int main(){ B b; C c; return 0;}
#include <iostream>#include <string>class String{public: String() : marker_(0x047F4578) {} std::string data() const { return 0x047F4578 == marker_ ? "Constructed" : "Not constructed"; }private: int marker_;};template<class Derived>class A{public: A(Derived* d){d->init();}};class B : public A<B>{public: B():A<B>(this){} void init(){std::cout << "B: String member " << string_.data() << std::endl;}private: String string_;};int main(){ B b; return 0;}