Hello!!
Assume you have the following AccountForStudent class
class AccountForStudent
{
public:
AccountForStudent ();
AccountForStudent (long, double);
~AccountForStudent ;
long getNumber() const
private:
Student * stud_;
double balance_;
};
and you have a stand alone function create defined as
AccountForStudent create(long number )
{
AccountForStudent local(number, 0.0);
return local;
}
// and you instansiate an object called global of class AccountForStudent in
this way
AccountForStudent global;
//and you call this stand alone function create in this way
global = create(300);
. . . .
Now to my question I just want to check with you that I have understood it
right.
When this function create is called a local object named local is created in
the function.
When the function create is doing return local the copy constructor is
called and
a temporary object is created lets call it temp. Now the object temp and
local is sharing the Student object.
We can imagine the code behind is something like
AccountForStudent temp(local);
this mean an object temp is created and initialized using object local.
This object temp is assigned to object global using the assignment operator.
After this statement global = create(300);
the temp object destructor is called and here we do delete Student leaving
the global object without any Student object.
Give me some feedback on this please.
Correct me if I'm wrong in some place.
//Tony