Search code examples
c++arrayspointersboostshared-ptr

Learning to use Boost Shared Pointers, Console Output Correct?


I have a few classes: Array, Shape, Point. A point is a type of shape.

I've created an Array of ShapePtr, and assigned a Point to the first element. When I print the first element of the Array, it just displays a strange number (the memory location, I suppose?). Is this normal? Is there a way to print the Point assigned to that position? Or is the Point already destroyed? My main is as follows:

{   

// Typedef for a shared pointer to shape
typedef boost::shared_ptr<Shape> ShapePtr;

// a typedef for an array with shapes stored as shared pointers.
typedef Array<ShapePtr> ShapeArray;

ShapeArray my_ShapeArray;

ShapePtr my_Point (new Point(3.1459));

my_ShapeArray[0] = my_Point;

cout  << my_ShapeArray[0] << endl;

return 0;

}

The output is (I have some comments for the constructors and destructors):

Array contructor call (default)

Shape contructor call (default)

Point contructor call (default)

002DDA20

Point destructor call

Shape destructor call

Array destructor call

I'm a little confused, as it seems the destructors are called after I print the point, but instead of printing the Point, it just seems to print the memory address. Could you please help me understand? Thanks!


Solution

  • A pointer is (roughly) a memory location, so when you print the pointer, that is what you get. If you want the object at that memory location instead, you dereference it by putting an asterisk in front:

    cout  << *my_ShapeArray[0] << endl;