operator Type() vs Type operator()

2022-02-11 08:29:31 浏览数 (1)

问题

比如 int operator() vs operator int(),这两者有什么区别?

回答

int operator() 是函数调用运算符(Function Call Operator),比如,

代码语言:javascript复制
struct Foo
{
    int operator()(int a, int b)
    {
        return a   b;
    }
};

...

Foo foo;
int i = foo(1, 2);  // Call the object as a function, and it returns 3 (1 2)

operator int() 是类型转换运算符(Type Conversion Operator),比如,

代码语言:javascript复制
struct Bar
{
    operator int()
    {
        return 123;
    }
};

...

Bar bar;
int i = bar;  // Calls the conversion operator, which returns 123

0 人点赞