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;
    }
};

will the copy constuctor still work properly if i had decided to implement it was follows

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

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

Recommended Answers

No. By changing the type of the parameter, you've changed the syntax required to call it. Additionally, you've broken the code that is contained within it. You would have to swap the "arrow" operator '->' for the "dot" operator '.'

Also, because it's now a non-const pointer to a …

Jump to Post

All 2 Replies

If you use the second one, it will not work normally as a copy constructor. Instead of passing it a Cent, you'll have to pass it a "Cent*", because while a Cent can be implicitly converted into a "Cent&", it cannot be converted implicitly into a "Cent*". You can keep the new copy constructor if you want, but if you want to be able to use a normal copy constructor and avoid strange errors, keep a default copy constructor (actually, your compiler may automatically generate a copy constructor, but Idunno for sure).

No. By changing the type of the parameter, you've changed the syntax required to call it. Additionally, you've broken the code that is contained within it. You would have to swap the "arrow" operator '->' for the "dot" operator '.'

Also, because it's now a non-const pointer to a non-const object instead of a const reference to an object, it's no longer const-correct which means you now have the ability to change the original object, and you shouldn't be able to, so it's less "safe".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.