P415 C++ Primer Plus (第六版)(待解决~~知道原理的同学请留言,多谢~~)

#include <iostream>
using namespace std;

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

class Cp
{
private:
  int a;
  double b;
public:
  Cp()
  {
    a = 1;
    b = 2.2;
  }

  operator int() const//this指针类型是const Cp*
  {
    return (int(3.6));
  }

  operator double() const//this指针类型是const Cp*
  {
    return b;
  }
  operator double()//this指针类型是Cp*
  {
    return 4.4;
  }
};

int main()
{
  Cp p1;//this 指针为 Cp* 是non-const的
  int i = int(p1);//为啥会调用operator double(),而不是operator int() const??难道是因为this的类型??
  //首先是如何确定int为int类型,int转换成double是可行的,
  cout << i << endl;

  system("pause");
  return 0;
}

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄