您当前的位置: 首页 >  c++

txwtech

暂无认证

  • 1浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

cc24a_c++_转换函数operator int

txwtech 发布时间:2020-02-21 21:03:54 ,浏览量:1

c++ cc24a_demo //转换函数,用来做转换操作符,int()括号里面必须是空的,必须定义为const,代码示范
#include 
#include 
using namespace std;

class Dog
{
public:
    Dog(string n, int a, double w) :name(n), age(a), weight(w) {}
    operator int() const//转换函数,用来做转化操作符,int()括号里面必须是空的,必须定义为const
    {
        return age;
    }
    operator double() const//转换函数
    {
        return weight;
    }
    operator std::string() const//转换函数
    {
        return name;
    }
private:
    int age;
    double weight;
    string name;
};

int main()
{
    int a, b;
    a = 10;
    b = a;
    Dog d("Bill",6,15.0);
    b = d; //d得到的是整形,调用转换函数,operator int()
    cout             
关注
打赏
1665060526
查看更多评论
0.0407s