我想撥打B::func2(int x, int y)
A::func1(x)
,如何申報?C++繼承,如何在基類的方法中調用子類的方法?
class A {
public:
void func1(int x);
virtual void func2(int x, int y); // do not need implement in A
};
class B : public A {
public:
void func2(int x, int y) override; // override
};
void A::func1(int x) {
int y;
func2(x, y); // I want to use the version of A's subclass
}
int main()
{
B b;
b.func1(x); // call A::func1(x) first then call B::func2(x, y)
}
它不會編譯,而是下面的錯誤信息顯示
> clang++ test.cpp -std=c++11
Undefined symbols for architecture x86_64:
"typeinfo for A", referenced from:
typeinfo for B in test-bbac7f.o
"vtable for A", referenced from:
A::() in test-bbac7f.o
NOTE: a missing vtable usually means the first non-inline virtual member > function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
您顯示的代碼不會出現這種情況嗎?你如何測試它?你得到了什麼結果,你期望得到什麼結果? –
爲什麼你要在'A :: func1()'中調用'B :: func2()'?如果實際對象不是'B',你需要什麼? – Peter