这是我的简化代码,需要您对实现不佳的多态性提出宝贵意见。
class X
{
public:
void test();
protected:
virtual void foo() const = 0;
};
class Y : public X
{
public:
void foo(){ cout << "hello" << endl; }
};
int main()
{
X *obj = new Y();
}
我在编译时收到以下错误。
test.cpp: In function ‘int main()’:
test.cpp:23: error: cannot allocate an object of abstract type ‘Y’
test.cpp:14: note: because the following virtual functions are pure within ‘Y’:
test.cpp:9: note: virtual void X::foo() const