如果有誰能夠告訴我這到底是怎麼回事我會很感激的論點純虛函數:說我聲明如下相同類別
class Base {
public:
virtual void member(Base b) = 0;
};
這給下面的編譯器錯誤:
pvf.cpp:3:18: error: cannot declare parameter ‘b’ to be of abstract type ‘Base’
virtual void member(Base b) = 0;
^
pvf.cpp:1:7: note: because the following virtual functions are pure within ‘Base’:
class Base {
^
pvf.cpp:3:18: note: virtual void Base::member(Base)
virtual void member(Base b) = 0;
但是,如果我通過引用傳遞,它編譯沒有問題:
class Base {
public:
virtual void member(Base& b) = 0;
};
此外,我想李柯在派生類中實現()成員作爲
class Base {
public:
virtual void member(Base& b) = 0;
};
class Derived : public Base {
public:
void member(Derived& d) {};
};
int main() {
Derived d;
}
不過,(明顯?)我得到
pvf.cpp: In function ‘int main()’:
pvf.cpp:12:14: error: cannot declare variable ‘d’ to be of abstract type ‘Derived’
Derived d;
^
pvf.cpp:6:8: note: because the following virtual functions are pure within ‘Derived’:
class Derived : public Base {
^
pvf.cpp:3:15: note: virtual void Base::member(Base&)
virtual void member(Base& b) = 0;
如果你聲明'member(Base b)'(沒有引用),這意味着每次有人調用'member'時,參數都會被傳值並被複制(如果傳入的對象實際上是一個派生的',它會在複製過程中得到*切片*)。 – Cameron
是的。如預期的那樣。使用'Base&'。你有什麼問題? –