class Sample {
private:
int data = 0;
std::string name = nullptr;
public:
Sample(int m_data, const std::string& pName) : data(m_data), name(pName) {
std::cout << "Sample(int, const std::string&): " << name << "\n";
}
Sample(const Sample& rhs) : data(rhs.data), name(rhs.name) {
std::cout << "Sample(const Sample&): " << name << "\n";
}
~Sample() {
std::cout << "~Sample(): " << name << "\n";
}
}
Sample TestFunc(int param) {
Sample a(param, "a");
return a;
}
I've made a class like this and called TestFunc(20);
in main(). Then following output came out.
Sample(int, const std::string&): a
~Sample(): a
I expected that a temporary Sample
object is made so copy constructor should be called, but it wasn't.
This code has compiled with GCC & Clang, and with MSVC, copy constructor is called as I expect.
Sample(int, const std::string&): a
Sample(const Sample&): a // temporary object constructor
~Sample(): a
~Sample(): a // temporary object destructor
What is the problem with GCC/Clang?