When overloading operator ->
, one would eventually have to return a pointer. If this pointer points to an object created inside the function like:
struct Something {
char c;
Something(char k){
c = k;
}
};
struct Hello {
int a;
Something* operator->(){
Something s(a);
return &s;
}
};
Would then dereferencing this result in undefined behavior (as Can a local variable's memory be accessed outside its scope?):
Hello h {5};
cout << h->c;
If so, can this be resolved, still using operator->
?