0
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;
    }
};
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Fbody
0

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).

0

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".

Edited by Fbody: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.