关注公众号

关注公众号

手机扫码查看

手机查看

喜欢作者

打赏方式

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

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

2020.9.28

输出结果(没有编译通过)

root@txp-virtual-machine:/home/txp# g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:21:14: error: cannot convert ‘Test’ to ‘int’ in initialization
     int i = t;
             ^

代码类类型与类类型之间的转换:

#include <iostream>
#include <string>
class Value{
};
class Test{
public:
   Test()
   {
   }
   Test(int i)
   {
   }
};
int main()

    Test t;
    Value i;
    t=i;
    return 0;

输出结果(暂时还是不行,编译不通过):

root@txp-virtual-machine:/home/txp# g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:27:7: error: no match for ‘operator=’ (operand types are ‘Test’ and ‘Value’)
     t=i;
      ^
test.cpp:27:7: note: candidate is:
test.cpp:9:7: note: Test& Test::operator=(const Test&)
class Test{
      ^
test.cpp:9:7: note:   no known conversion for argument 1 from ‘Value’ to ‘const Test&’

说明:上面的例子,我们只是简单的按照实际角度出发,发现确实有写转换行不通。那么真理到底是怎样的?我们接着往下看


推荐
关闭