IvanKeller

导航

类的转换函数调用的优先级与是否用const修饰的关系

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

#include <iostream>
using namespace std;

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;
}

posted on 2019-05-10 15:34  IvanKeller  阅读(223)  评论(0)    收藏  举报