I have the following class
template <typename T> class Item {
public:
T item;
Item () : item( T() ) {}
Item (T arg) { this->item = arg;}
operator T () const {
return item;
}
};
Now I want to write an assignment operator that also changes the type of an object. Is it possible? I was looking for him, but nothing came of it (which, incidentally, makes me think that maybe I'm a little crazy).
To make this clear, let's say I have the following two objects:
Item <int> intItem = 3;
Item <double> doubleItem = 3.4;
I want to write
intItem = doubleItem;
And after that, I want the intItem type to be Item<double>.
If I only needed the “classic” assignment operator, it would work fine if inside my class I would have something like
Item<int>& operator= (const Item<double> & var) {
this->item = var.item;
return *this;
}
A double value will be rounded, but it will work. Oh, and the intItem type would remainItem<int>
P.S.: , . Item "- " .
, , , .