相关疑难解决方法(0)

为什么在上下文转换中没有发生显式的bool()转换

如果以下测试程序

#include <iostream>

class A {
public:
    A() {}
    explicit operator bool() const {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
        return true;
    }
//    explicit operator bool() {
//        std::cout << __PRETTY_FUNCTION__ << std::endl;
//        return true;
//    }
    const operator int() const {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
        return 1;
    }
    operator int() {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
        return 1;
    }
};

int main() {
    A a;
    if (a) {
        std::cout << "bool()" << std::endl;
    }
    if (a + …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading c++11

16
推荐指数
1
解决办法
1455
查看次数

标签 统计

c++ ×1

c++11 ×1

operator-overloading ×1