2017-04-16 92 views
0

我回顧了nullptr一些代碼,結果發現:「內聯運算符T *()const」是什麼意思?

const 
class nullptr_t 
{ 
public: 
    template<class T> 
    inline operator T*() const 
     { return 0; } 

    template<class C, class T> 
    inline operator T C::*() const 
     { return 0; } 

private: 
    void operator&() const; 
} nullptr = {}; 

什麼inline operator T*() constinline operator T C::*() const是什麼意思?

它們的工作方式與inline T operator *() constinline T operator C::*() const相同嗎?

爲什麼不在聲明中指定返回類型?

回答

2

它們是用戶定義的轉換運算符函數。

一個更簡單的功能:

struct Foo 
{ 
    inline operator int() const { return 0; } 
}; 

既然你可以使用

Foo f; 
int i = f; // Invokes f.operator int() and initializes i with 
      // return value. 

對於你的類,它是nullptr匿名類,它的意思是,它可以轉換成任何指針或成員函數指針。您可以使用:

int* ip = nullptr; 

它使用的第一個用戶定義的轉換操作符函數的返回值來初始化ip

struct Bar 
{ 
    void bar() {} 
}; 

void (Bar::*ptr)() = nullptr; 

它使用所述第二用戶定義的轉換運算符的函數的返回值來初始化ptr