• ### Announcements

#### Archived

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

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