#include
struct base { base(); };
struct derived : base { derived(); }
base::base()
{
cout << "\tbase 1: this=" << int(this) << " \n"; if (this="=" 0) this="(base*)27;" cout << "\tbase 2: this=" << int(this) << " \n"; } derived::derived() { cout << "\tderived 1: this=" << int(this) << " \n"; this="(this" ="=" 0) ? (derived*)43 : this; cout << "\tderived 2: this=" << int(this) << " \n"; } main() { cout << "base b;\n"; base b; cout << "new base b;\n"; new base; cout << "derived d;\n"; derived d; cout << "new derived d;\n"; new derived; cout << "at the end\n"; }
порождает вывод
base b;
base 1: this=2147478307
base 2: this=2147478307
new base;
base 1: this=0
base 2: this=27
derived d;
derived 1: this=2147478306
base 1: this=2147478306
base 2: this=2147478306
derived 1: this=2147478306
new derived;
derived 1: this=0
base 1: this=43
base 2: this=43
derived 1: this=43
at the end