c++ - Overloading assignment operator when the object is on the right-hand side of the assignment -


given following code:

template <typename t> struct wrapper {     t value;     wrapper(t val) : value(val) {} }  int main() {     wrapper<int> x(42);     int y = x; // need solution y = x.value     return 0;  } 

is there way implement assignment

int y = x; 

so means y = x.value .

i know overloading assignment operator not possible because applies object on left side of assignment , friend function 2 arguments not allowed standard.

if not possible overloading other operator, or using special tricks, how implement this, except invoking method provided wrapper class such as:

int y = x.get(); 

why not provide implicit conversion t

operator t() { return value; }  

this cause assignment function because compiler attempt convert right side of assignment t. implicit conversion allow succeed

note cause other conversions work besides assignment. example possible pass wrapper<t> instances t parameters. may or may not work particular scenario


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -