Z
Zongjun Qi
Hey,
In the book <Effective C++>, the author provides an example to prove
why we need "pass by reference". I redoed the example, and found
something interesting. The codes are:
##############################
#include <iostream>
class Student{
public:
Student(){
std::cout << "inside CTOR. this = " << this << std::endl <<
std::endl;
}
Student(const Student& rhs){
std::cout << "inside COPY CTOR. this = " << this << ", rhs =
"<< &rhs << std::endl << std::endl;
}
~Student(){
std::cout << "inside DTOR. this = " << this << std::endl <<
std::endl;
}
};
Student ReturnStudent(Student s){
std::cout << "inside 'ReturnStudent' function" << std::endl <<
std::endl;
return s;
}
int main(){
Student Plato;
ReturnStudent(Plato);
std::cout << "outside 'ReturnStudent' function" << std::endl <<
std::endl;
}
###########################
The thing is when you run it, I got the following output:
inside CTOR. this = 0xbfff6e30
inside COPY CTOR. this = 0xbfff6e10, rhs = 0xbfff6e30
inside 'ReturnStudent' function
inside COPY CTOR. this = 0xbfff6e20, rhs = 0xbfff6e10
inside DTOR. this = 0xbfff6e20
inside DTOR. this = 0xbfff6e10
outside 'ReturnStudent' function
inside DTOR. this = 0xbfff6e30
################################
Which means DTOR of the returned value called actually 'BEFORE' DTOR of
's' called inside the ReturnStudent function, and this is differenct to
the author said, and which I originally believed.
I am using G++ 3.2.3, Linux 2.4.21-32.EL. Compile the program using
"g++ -g main.cpp", not without any optimization.
In the book <Effective C++>, the author provides an example to prove
why we need "pass by reference". I redoed the example, and found
something interesting. The codes are:
##############################
#include <iostream>
class Student{
public:
Student(){
std::cout << "inside CTOR. this = " << this << std::endl <<
std::endl;
}
Student(const Student& rhs){
std::cout << "inside COPY CTOR. this = " << this << ", rhs =
"<< &rhs << std::endl << std::endl;
}
~Student(){
std::cout << "inside DTOR. this = " << this << std::endl <<
std::endl;
}
};
Student ReturnStudent(Student s){
std::cout << "inside 'ReturnStudent' function" << std::endl <<
std::endl;
return s;
}
int main(){
Student Plato;
ReturnStudent(Plato);
std::cout << "outside 'ReturnStudent' function" << std::endl <<
std::endl;
}
###########################
The thing is when you run it, I got the following output:
inside CTOR. this = 0xbfff6e30
inside COPY CTOR. this = 0xbfff6e10, rhs = 0xbfff6e30
inside 'ReturnStudent' function
inside COPY CTOR. this = 0xbfff6e20, rhs = 0xbfff6e10
inside DTOR. this = 0xbfff6e20
inside DTOR. this = 0xbfff6e10
outside 'ReturnStudent' function
inside DTOR. this = 0xbfff6e30
################################
Which means DTOR of the returned value called actually 'BEFORE' DTOR of
's' called inside the ReturnStudent function, and this is differenct to
the author said, and which I originally believed.
I am using G++ 3.2.3, Linux 2.4.21-32.EL. Compile the program using
"g++ -g main.cpp", not without any optimization.