Temporary object created by calling constructor
Hi, I was wondering if this idea I have is correct, I couldn't find an answer online :(
if i had a simple class
So since the coolFunction(Dog Object) takes an object of type Dog, instead of declaring a
void coolFunction(Dog) ;//Receives object of type Dog as parameter
int y ;
new object Dog dog1
and do this
I should be able to do this
coolFunction(dog1); ...//first method
I believe that that the second method will create a temporary object in memory by calling the Vector(x,y) constructor,.. and when that copy is passed to the coolFunction(...) the object is then destroyed.
coolFunction(Dog(4,4)) ; //second method
Can someone tell me if that is wrong or right?
Or if that is a good practice or idea..i know in C# the same concept is used or it is acceptable.
I promised to get back to this.
With better understanding of Copy Constructor.
I want to clarify the difference between doing this
The compiler does not see Dog(4,4) as a already existing object, so therefore did not have any reason to invoke the copy constructor.
a.coolFunction(Dog(4,4) ) ;
as a result it calls this constructor
but for this piece of code a.coolFunction(b)
Dog(int x, int y) : x(x), y(y)
std::cout << "Dog(int, int) of " << this << std::endl;
the compiler sees the reason to use a copy constructor to copy the already defined object.
That's an attempt to explain it to myself...