Skip to content

Big object as member of class

troussil edited this page Feb 10, 2012 · 2 revisions

Let class BigObject{ ... };

variable: BigObject o;

constant: const BigObject o = ...;

pointer: BigObject* o;

constant pointer: BigObject* const o = ...;

pointer on constant data: const BigObject* o;

constant pointer on constant data: const BigObject* const o = ...;

reference: BigObject& o = ...;

constant reference: const BigObject& o = ...;

What type should I use ?

Goal: since the object is big, we do not want to copy it (or the less often possible)

  • if the member owns the object (which is only stored in this class):

  • if the class is not default-constructible (the object is always initialized at construction) and not assignable (the object is never copied): simple variable or constant if it should not be modified.

  • otherwise:

    • pointer (be careful of the memory management at initialization, assignment and destruction)

    • smart pointer

  • if the member is an alias (of an object that is stored elsewhere):

    • if the class is assignable: - pointer or pointer on constant data if it should not be modified

      • if the class is NOT assignable:
        • if the object may be not initialized at construction:

          • constant pointer or constant pointer on constant data if it should not be modified
        • if the object is always initialized at construction:

          • reference or constant reference if it should not be modified