I am learning C++, and this is my first attempt to understand how copy constructors work:
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents=0)
{
m_nCents = nCents;
}
// Copy constructor
Cents( Cents cSource)
{
m_nCents = cSource.m_nCents;
}
};
int main()
{
Cents cMark(5); // calls Cents constructor
Cents cNancy = cMark; // calls Cents copy constructor!
return 0;
}
But I get this error:
Error 1 error C2652: 'Cents' : illegal copy constructor: first parameter must not be a 'Cents'
What is wrong in my copy constructor?
I checked that if in the constructor I pass the parameter by reference, then it is compiling well, but won't work in the way I am doing. Why it is like that?