C++ 什么是;内联运算符T*()const“;什么意思?

C++ 什么是;内联运算符T*()const“;什么意思?,c++,C++,我查看了nullptr上的一些代码,发现: 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 o

我查看了nullptr上的一些代码,发现:

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 = {};
const
类nullptr\t
{
公众:
模板
内联运算符T*()常量
{返回0;}
模板
内联运算符tc::*()常量
{返回0;}
私人:
void运算符&()常量;
}nullptr={};
内联运算符T*()常量和
内联运算符tc::*()常量是什么意思

它们的工作方式是否与
内联T运算符*()常量
内联T运算符C::*()常量
相同


为什么不在声明中指定返回类型?

它们是用户定义的转换运算符函数

更简单的功能:

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