S
Shea Martin
Trying to avoid a temp object, and c-style funcitons
(i.e., void doSomethingTo(Type* thisObject) ).
<referece: code below>
Is it safe to return a reference to an internally created object? Is
obj's destructor called at end of main or at end of fromString()?
This is how I uderstand it: o's copy constructor is called with obj as
the arg, and I have avoided a temporary instance of Object.
Is this safe?
Thanks
<code>
class Object
{
public:
static Object &fromString(std::string str)
{
Object obj;
//process string, and set attributes of obj
return obj;
}
};
int main()
{
Object o = Object::fromString("attribute1,attribute2");
cout << "att1 is " << o.getAtt1()
<< "att2 is " << o.getAtt1()
<< "att3 is " << o.getAtt1() << endl;
}
</code>
(i.e., void doSomethingTo(Type* thisObject) ).
<referece: code below>
Is it safe to return a reference to an internally created object? Is
obj's destructor called at end of main or at end of fromString()?
This is how I uderstand it: o's copy constructor is called with obj as
the arg, and I have avoided a temporary instance of Object.
Is this safe?
Thanks
<code>
class Object
{
public:
static Object &fromString(std::string str)
{
Object obj;
//process string, and set attributes of obj
return obj;
}
};
int main()
{
Object o = Object::fromString("attribute1,attribute2");
cout << "att1 is " << o.getAtt1()
<< "att2 is " << o.getAtt1()
<< "att3 is " << o.getAtt1() << endl;
}
</code>