一道类的强制类型转换题,请帮忙!operator
#include<iostream.h> class Cat; class Person { public: int age; }; class Cat { public: int age; };
void main(void) { Person p1; p1.age=50; Cat c1; c1=p1; cout<<c1.age<<endl; } 请问如何将Person类强制转换成Cat类,使c1=p1覆值成功
谢谢楼上的兄台。我已经搞定了 #include<iostream.h> class Cat { public: int age; /*operator =(Person p) { age=p.age; }*/ Cat(int age) { this->age=age; } }; class Person { public: int age; Person(int age) { this->age=age; } operator Cat() { return Cat(this->age); }
}; void main(void) { Person p1(20); Cat c1(10); c1=p1;//1> c1=(Cat)p1; 2> c1=p1 cout<<c1.age<<endl; }