Prefix and Postfix Overloading
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!
He is probably using the default copy constructor, which would be declared that way if the compile didn''t give you one.
Declare one yourself that way, add a debug statement (like cerr << "copy constructor" << endl) and you''ll know if it is what happens.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement