下面是我的簡化代碼,它需要您關於實施不良多態性的寶貴意見。抽象基類擴展
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