![](/img/trans.png)
[英]Achieve operator overloading without using member functions and friend functions
[英]Using operator() in member functions
我在我的一個類中重載了operator(),我想在另一個成員函數中使用它。
class A {
public:
void operator()();
void operator()(double x);
};
void A::operator()() {
// stuff
};
void A::operator()(double x) {
// stuff with other members and x
this->operator();
};
這一行 - this->operator()
不起作用。 我只想使用我定義的運算符作為我的類A
的成員函數。 我得到的錯誤是: Error 1 error C3867: 'A::operator ()': function call missing argument list; use '&A::operator ()' to create a pointer to member
Error 1 error C3867: 'A::operator ()': function call missing argument list; use '&A::operator ()' to create a pointer to member
你應該寫:
void A::operator()(double x) {
// stuff with other members and x
this->operator()();
};
第一個()
是運算符的名稱,第二個是調用本身的名稱:這是錯誤消息中缺少的(空)參數列表。
在指向對象的指針上調用成員函數的語法是:
ptr->memberName();
在你的情況下,成員名稱是operator()
,所以你想寫
ptr->operator()();
或者用於其他變體
ptr->operator()(1.0); // operator() accepting single argument of type double
定義operator()
同樣如此 - 你到處都缺少一對parenteses。
經驗法則:
運算符重載函數的名稱始終為 operator@
,其中@
為重載運算符。
要直接通過名稱調用operator-overload-function,請鍵入operator@(params)
,在您的情況下,鍵入operator() (...)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.