Prefix and Postfix Overloading

Started by
1 comment, last by Nacho 22 years, 4 months ago
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!
Advertisement
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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!!

This topic is closed to new replies.

Advertisement