Skip to content

Instantly share code, notes, and snippets.

@fortheday
Last active January 29, 2018 05:38
Show Gist options
  • Save fortheday/b0c9b513f9c636b9706edaf1f609aafe to your computer and use it in GitHub Desktop.
Save fortheday/b0c9b513f9c636b9706edaf1f609aafe to your computer and use it in GitHub Desktop.
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