关注公众号

关注公众号

手机扫码查看

手机查看

喜欢作者

打赏方式

微信支付微信支付
支付宝支付支付宝支付
×

C++之类型转换函数(七)

2020.9.28

输出结果:

root@txp-virtual-machine:/home/txp# g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:42:15: error: conversion from ‘Test’ to ‘Value’ is ambiguous
    Value v = t;
              ^
test.cpp:41:10: note: candidates are:
    Test t(100);
         ^
test.cpp:31:5: note: Test::operator Value()
    operator Value()
    ^
test.cpp:14:6: note: Value::Value(Test&)
     Value(Test& t)

3、小结:

无法抑制隐式的类型转换函数调用

类型转换函数可能与转换构造函数起冲突

当然工程中可能比较习惯用 Type toType()的公有成员代替类型转换函数(就是换了种写法)


推荐
关闭