I recently tested the deep and dark corners of C ++, and I got confused in one subtle question. My test is so simple:
howmany t(howmany());
howmany t = howmany();
My expectation is from the line above; the first call to the constructor howmany()will create one temporary object, and then the compiler will use this temporary object with the copy constructor to create an instance of t . However, the compiler output really confused me, because the output shows only one constructor call. My friends mentioned me about optimizing the compiler profile, but we are not sure about that. I want to know what is going on here?
The result of task 2 is given below. Problem 1 goes completely beyond the scope of the object, because the compiler behaves like a function pointer declaration.
howmany()
~howmany()
:
class howmany {
public:
howmany() {
out << "howmany()" << endl;
}
howmany(int i) {
out << "howmany(i)" << endl;
}
howmany(const howmany& refhm) {
out << "howmany(howmany&)" << endl;
}
howmany& operator=(const howmany& refhm) {
out << "operator=" << endl;
}
~howmany() {
out << "~howmany()" << endl;
}
void print1() {
cout << "print1()" << endl;
}
void print2() {
cout << "print2()" << endl;
}
};