I'm new to c++ and I'm reading c++ from several text books but I still can't figure out somting please help me :)
say u have class A and class B that holding class A as a memebr
1 2 3 4 5 6 7 8 9 10 11 12 13
class B
{
private :
A a;
public:
//...constructor and destructor go here...
A & GetARef() {return a;}
A GetA() {return a;}
};
what is the difference between the 2 get methods? I mean I know that one returns a reference and the other one return same object so what the difference and when should I use those methods?
The first method does not use the copy constructor and allows to change the original object. While the second method uses the copy constructor to create a temporary object and therefore does not allow to change the original object.
Thank u for your answer. So the second method returns a copy of a?
so after calling the second method I have 2 A objects? and if it is a copy of a why cant I change it? after all its a copy i.e. different object