copy constructor

class Cents
{
private:
    int m_nCents;
public:
    Cents(int nCents=0)
    {
        m_nCents = nCents;
    }

    // Copy constructor
    Cents(const Cents &cSource)
    {
        m_nCents = cSource.m_nCents;
    }
};


In above c++ code  why parameter cSource(const Cents &cSource) is passed as reference?

Comments

  1. if we pass this argument as pass by value then copy constructor will call every time during copy argument. that cause struck in infinite loop. so stack overflow.

    ReplyDelete
  2. @sandeep yeah correct explanation..:)

    ReplyDelete

Post a Comment

Popular posts from this blog