pthirdbox is a pointer variable of type, pointer to box, the '*' denotes this variable a pointer. a pointer is a 4 byte field that contains an address which points to an instance of the specified type attached to it, in this case an object of type box. The way that you have chosen to write your declaration/definition "box* pthirdbox(&firstbox);" is function initialization form. what is happening is you are declaring a pointer of type, pointer to box, and initializing it to point to the object firstbox by assigning the address of the first box, with the '&' operator which is the address of operator, to the pointer pthirdbox. It is not a new instance of a third box. In your cout statements: cout << "volume of first box = " << firsttbox.volume() << endl; and cout << "volume of third box = " << pthirdbox->volume() << endl; notice how you dereference the class member volume() in the first one you dereference it with the '.'(dot) Operator which is the direct member access operator and in the second you dereference it with '->' which is the indirect member access operator. So you are indirectly accessing the class member volume() of the firstbox object thru the pointer psthirdbox via the -> operator.
|