I have a member function that needs to return multiple references of char member variables.
class Foo {
public:
Foo() {// ... constructor }
std::string& func();
private:
char ch1 = 'a', ch2 = 'b', ch3 = 'c';
};
The code below is something would obviously not fulfill my purpose, because the lifetime of the local variable is limited to the scope of the function call:
std::string& func()
{
std::string total = "";
total += this-> a;
total += this-> b;
total += this-> b;
return total;
}
I would also prefer not to use the method of allocating memory for the local variable using new
, then having to delete
it after.
I also would not prefer to add a new member variable to the class that is used in the member function and returned.
First of all, is doing this even possible? If so, how can I achieve this?
Many thanks for help.