#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; }
#include<iostream.h> class Cat; class Person { public: int age; };
class Cat { public: int age; Cat &operator =(Person p1) { Cat temp; temp.age = p1.age; return temp; } };
void main() { Person p1; Cat c1; p1.age = 50; c1=p1; cout<<c1.age<<endl; } 我用c free 3.5 结果为575,怪怪