2011-05-12 90 views

回答

2

這是使用指針成員變量時使用的運算符。見here

2

。*解除引用類成員的指針。當你需要調用一個函數或訪問另一個類中包含的值時,它必須被引用,並且在該進程中創建一個指針,該指針也需要被刪除。 。*運算符會這樣做。

3

簡單的例子:

class Action 
{ 
    public: 
     void yes(std::string const& q) { std::cout << q << " YES\n"; } 
     void no(std::string const& q) { std::cout << q << " NO\n"; } 
}; 

int main(int argc, char* argv[]) 
{ 
    typedef void (Action::*ActionMethod)(std::string const&); 
         // ^^^^^^^^^^^^ The name created by the typedef 
         // Its a pointer to a method on `Action` that returns void 
         // and takes a one parameter; a string by const reference. 

    ActionMethod method = (argc > 2) ? &Action::yes : &Action::no; 
    Action action; 

    (action.*method)("Are there 2 or more parameters?"); 
    // ^^^^^^ Here is the usage. 
    // Calling a method specified by the variable `method` 
    // which points at a method from Action (see the typedef above) 
} 

作爲附帶說明。我很高興你不能超載這個運營商。 :-)