简体   繁体   English

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

[英]what does “inline operator T*() const” mean?

I reviewed some codes on nullptr and found that: 我查看了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 = {};

what do inline operator T*() const and inline operator TC::*() const mean? inline operator T*() constinline operator TC::*() const是什么意思?

Do they work the same way as inline T operator *() const or inline T operator C::*() const ? 它们的工作方式与inline T operator *() constinline T operator C::*() const吗?

Why not specify the return type in the declaration? 为什么不在声明中指定返回类型?

They are user defined conversion operator functions. 它们是用户定义的转换操作符函数。

A simpler function: 一个更简单的功能:

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

Given that you can use 鉴于你可以使用

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

For you class, which is an anonymous class for nullptr , what it means is that it can be converted to any pointer or member function pointer. 对于你的类,它是nullptr的匿名类,它的意思是它可以转换为任何指针或成员函数指针。 You can use: 您可以使用:

int* ip = nullptr;

It uses the return value of the first user defined conversion operator function to initialize ip . 它使用第一个用户定义的转换运算符函数的返回值来初始化ip

struct Bar
{
   void bar() {}
};

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

It uses the return value of the second user defined conversion operator function to initialize ptr . 它使用第二个用户定义的转换运算符函数的返回值来初始化ptr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM