这是我正在尝试做的(此代码不起作用):
class Base
{
virtual Base *clone() { return new Base(this); }
virtual void ID() { printf("BASE");
};
class Derived : publc Base
{
virtual Base *clone() { return new Derived(this); }
virtual void ID() { printf("DERIVED"); }
}
.
.
Derived d;
Base *bp = &d;
Base *bp2 = bp->clone();
bp2->ID();
我想看到打印出“派生”……我得到的是“基础”。我是一名长期的 C 程序员,并且对 C++ 相当有经验……但我在这方面没有取得任何进展……任何帮助将不胜感激。