Constructor Initialization/operator overloading

This is a discussion on Constructor Initialization/operator overloading within the C++ Programming forums, part of the General Programming Boards category; I've got a couple of questions, I'm just a little curious as to what is going on, I am doing ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    48

    Constructor Initialization/operator overloading

    I've got a couple of questions, I'm just a little curious as to what is going on, I am doing a workshop for teach yourself c++ in 21 days. My code works but it differs from the answers.

    First of all I am to write a class, its functions and a single variable easy enough, I am also
    to implement 2 constructors a copy constructor and prefix and postfix operators. Then I am to
    change the 1 variable to be on the free store, and fix everything accordingly.

    The questions I have are regarding the initialization stage of the constructors and the prefix/
    postfix operator functions.

    In my contructors before using the free store I have...


    Code:
    SimpleCircle::SimpleCircle():
    itsRadius(5)
    {}
    
    SimpleCircle::SimpleCircle(USHORT r):
    itsRadius(r)
    {}

    Default initializes to 5. The answers are the same, but...

    after the free store I have...

    Code:
    SimpleCircle::SimpleCircle():
    itsRadius(new USHORT(5))
    {}
    
    SimpleCircle::SimpleCircle(USHORT r):
    itsRadius(new USHORT(r))
    {}
    The answer is...


    Code:
    SimpleCircle::SimpleCircle()
    {
    	itsRadius = new USHORT(5);
    }
    
    SimpleCircle::SimpleCircle(USHORT r)
    {
    	itsRadius = new USHORT(r);
    }
    Is there any difference between the 2 methods? Preferred or otherwise? Like I said my code works.


    Onto the prefix/postfix.

    Before the free store change I had...

    Code:
    const SimpleCircle& SimpleCircle::operator++()
    {
        ++itsRadius;
        return *this;
    }
    
    const SimpleCircle SimpleCircle::operator++(int)
    {
        SimpleCircle temp;
        ++itsRadius;
        return temp;
    }
    The answer is...

    Code:
    const SimpleCircle& SimpleCircle::operator++()
    {
        ++(itsRadius);
        return *this;
    }
    
    const SimpleCircle SimpleCircle::operator++(int)
    {
        SimpleCircle temp(*this);
        ++(itsRadius);
        return temp;
    }
    Whats with the parenthesis? and do you have to put (*this) ?

    Ok after the free store change I have...

    Code:
    const SimpleCircle& SimpleCircle::operator++()
    {
        ++(*itsRadius);
        return *this;
    }
    
    const SimpleCircle SimpleCircle::operator++(int)
    {
        SimpleCircle temp;
        ++(*itsRadius);
        return temp;
    }
    The * to dereference itsRadius but in the answer...

    Code:
    const SimpleCircle& SimpleCircle::operator++()
    {
        ++(itsRadius);
        return *this;
    }
    
    const SimpleCircle SimpleCircle::operator++(int)
    {
        SimpleCircle temp(*this);
        ++(itsRadius);
        return temp;
    }
    The (*this) is there still, but itsRadius is not dereferenced! I tried it like this and got
    strange output so is there a mistake in the answers or could I have missed something else
    entirely?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    Quote Originally Posted by MattJ812
    Is there any difference between the 2 methods? Preferred or otherwise? Like I said my code works.
    Yes, in that your code initialises the member variables whereas the "model answer" uses assignment in the constructor body. Normally, we prefer to initialise rather than delay providing the initial value to assignment in the constructor body. In this case, the difference does not really matter.

    That said, if you were to initialise another member pointer using new (or new[]), there would be a problem in that you would be unable to recover if the second new or new[] failed. In this case, using assignment within a try block would be better, though not necessarily the best option. Read GotW #66: Constructor Failures.

    Quote Originally Posted by MattJ812
    Whats with the parenthesis?
    The parentheses are unnecessary. I would prefer to write ++itsRadius too.

    Quote Originally Posted by MattJ812
    and do you have to put (*this) ?
    Yes. What you are doing is default constructing a SimpleCircle object and returning that. What you should do is construct a copy of the current SimpleCircle object and return that. The "model answer" is correct in this respect. That said, I would have written post-increment as:
    Code:
    const SimpleCircle SimpleCircle::operator++(int)
    {
        SimpleCircle temp(*this);
        ++*this;
        return temp;
    }
    This implements post-increment in terms of pre-increment, and thus saves on maintenance such that you only need to update pre-increment.

    Quote Originally Posted by MattJ812
    The (*this) is there still, but itsRadius is not dereferenced!
    Yes, that looks like a copy & paste error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    48
    Thanks, is (*this) the same as = *this?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Quote Originally Posted by MattJ812 View Post
    Thanks, is (*this) the same as = *this?
    yes they are the same
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21