O'Reilly logo

SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Structures from the Heap

Structures are also frequently created with new. For instance:

aTapeElement *TapeElement = new aTapeElement;

This reads, “define a pointer to aTapeElement called TapeElement and initialize it with the location of the space created on the heap by new, with that space being the size of aTapeElement.”

You can use the period to select the member variables, but you must dereference the pointer first:

(*TapeElement).Operator = '+';
(*TapeElement).Operand = 234;

Because this is so frequently needed, C++ offers a shorthand for this expression, the pointer member selection (->) operator.

TapeElement->Operator = '+';
TapeElement->Operand = 234;

Naturally, you must always remember to delete a heap-allocated structure, but ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required