[Next] [Prev] [Up] [Top]

7.0 Classes

Use forward declarations for compile dependency minimization (GL)


Where practical, use a forward declaration for a class pointer within an include file. This obviates the need to include the "opaque" class's include file by this include file and all clients. This is appropriate where reference or pointer semantics are used for a class and no methods (or new and delete) are exposed in the original class' interface.

Note that this can introduce hard-to-find bugs whenever an explicit or implicit cast is encountered or in matching certain sets of overloaded functions.

Example:

class Item {};

class ItemA : public Item {};

class ItemB : public Item {};

class ItemAB;

class Head

{

public:

Item* first() ;

};

class HeadB : public Head

{

public:

ItemAB* first() { return (ItemAB*)(ItemB*)Head::first(); }

};

// At this point the casts above are effectively only a C-style

// reinterpretation of the pointer value.

class ItemAB : public ItemA, public ItemB {};

// Now any casts will be an inheritance conversion (adjustment of // the "this" pointer; the previous casts may have generated bad

// code!


No Title
[Next] [Prev] [Up] [Top]

Generated with CERN WebMaker

This site was grabbed using the TRIAL version of Grab-a-Site. This message does not appear on a licensed copy of Grab-a-Site.