Archived

This topic is now archived and is closed to further replies.

Nacho

Prefix and Postfix Overloading

Recommended Posts

Nacho    205
Hi! I´m reading Sams Teach Yourself C++ in 21 Days 4º Edition. Since most of you have already read this book maybe you can answer me this question. On page 300 there´s is a listing to overload the prefix and postfix operator. On line 33 he instantiates a temporary Counter object using the "this" pointer as a parameter for the constructor. The constructor is this one: Counter::Counter(): itsVal(0) {} My question is the following one, how can he instantiate the temp counter (and set its "itsVal" int private variable) using the this pointer if the constructor only creates objects and sets the "itsVal" to zero? I hope you understood my question. Thanks!

Share this post


Link to post
Share on other sites
Fruny    1658
He is probably using the default copy constructor, which would be declared that way if the compile didn''t give you one.


Counter::Counter( const Counter& rhs )
{
this->itsVal = rhs.itsVal;
}


Declare one yourself that way, add a debug statement (like cerr << "copy constructor" << endl) and you''ll know if it is what happens.

Share this post


Link to post
Share on other sites
Nacho    205
Thanks Fruny! I´m pretty new to C++ (altough I´ve been programming a lot in C for a year and some assembly for a couple of months), so certain things are new to me! I´ve been playing with the debugger and I think I got it! Thanks again!!

Share this post


Link to post
Share on other sites