Last active
January 29, 2018 05:38
-
-
Save fortheday/b0c9b513f9c636b9706edaf1f609aafe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CBase | |
{ | |
public: | |
virtual void Print() = 0; // pure virtual, abstract | |
}; | |
class CDerived0 : public CBase | |
{ | |
public: | |
CDerived0() { puts("new0"); } | |
CDerived0(const CDerived0 &) { puts("copy0"); } | |
virtual void Print() override { puts("0"); } | |
}; | |
class CDerived1 : public CDerived0 | |
{ | |
public: | |
virtual void Print() override { puts("1"); } | |
}; | |
void PolymorphTest() | |
{ | |
CDerived0 &r1 = CDerived1(); | |
r1.Print(); // 1 | |
CDerived1 *pInst = new CDerived1(); | |
(*((CDerived0 *)pInst)).Print(); // 1 | |
((CDerived0)*pInst).Print(); // 0 (copy0) | |
pInst->CDerived0::Print(); // 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment