Protecting object members
Plenty of questions lately!
Ok, if I have a member of a class that has private scope and is a pointer (for the sake of this question it can be of any type); I have another object that is requesting information from the first object. The only way I can think of to protect the integrity of the private member is to copy to a temporary variable of the same data type and return a copy of it. Is this the most efficient way of doing this?
Code:
class foo
{
private:
Someobject* so;
public:
...
Someobject getData(void); // Return a copy of the so data member
};
Someobject foo::getData(void)
{
Someobject temp;
// Assume that Someobject has an overloaded operator= defined
temp = so;
// Return a copy of the member object
return temp;
}