Code:
#include<iostream> using namespace std; class Cents { public: int m_nCents; Cents(int nCents=0):m_nCents(nCents) { cout<<"Calling normal constructor with value:"; m_nCents = nCents; cout<<m_nCents<<endl; } // Copy constructor Cents(const Cents& cSource) { cout<<"Calling copy construct\n"; m_nCents = cSource.m_nCents; std::cout<<m_nCents<<std::endl; } }; int main(){ Cents obj2 = 37; cout<<"Value of m_nCents with sobject2:"<<obj2.m_nCents; return 0; }
o/p is
Calling normal constructor with value:37
Value of m_nCents with sobject2:37
Question is :
Why is the overloaded copy constructor that I have written not getting called here?Internally default copy constructor is getting called.Thats why we get
value of obj2.m_nCents as 37.
~
~
Comment