PointerGuide

Chair for Computer Aided Medical Procedures & Augmented Reality
Lehrstuhl für Informatikanwendungen in der Medizin & Augmented Reality

Back to main page.

PointerGuide

Definitions

Primitive Types

  • int, float, double, char, bool, etc.
  • enums
  • pointer
  • smart-pointer (because the smart pointer semantic wouldn't work out otherwise)
  • classes, which are explicit wrappers of some primitive type and contain no further datamembers (e.g. trinary logic etc.)

Non primitive types

  • std::string
  • any other structs and classes (insbesondere structs und classes die nur zufällig aus einem primitiven Datenfeld bestehen)

Arguments to Methods/Functions

Call-by-Value

  • primitive data types
  • no change of the argument by the called function desired

const reference (const &)

  • non primitive data types
  • no change of the argument by the called function desired
  • no transferal of ownership -- object will be copied by the called function or forgotten

reference (&)

  • any data type
  • modification of the data by the called function is desired
  • no transferal of ownership -- object will be copied by the called function or forgotten
  • for creation of new objects as return values smart pointers should be used

Smart-Pointer

  • any data type
  • transferral of ownership: the called function now owns the object
  • called function may copy the pointer and thus extend the lifetime of the object indefinitely

(const) Pointer

  • no ownership
  • same as (const) reference, but parameter explicitly can be NULL and it is copy/assignable (for containers)
  • try to replace by weak_ptr if object is owned by a shared_ptr possible

Return Values of Methods/Functions

Return-by-Value

  • primitive data types

const reference

  • nicht-primitive Datentypen
  • no chnange by the calling function desired
  • calling function may only use the returned object for a short time (till the next non-const-call) and has to copy it or forget it then.

reference

  • any data type
  • change by the calling function possible
  • no change of ownership

Smart-Pointer

  • any data type
  • (co-)ownership is transferred to the calling method
  • calling function may copy the pointer and thus extend the lifetime of the object indefinitely
  • perfect for returning new'ed objects from functions

Pointer

  • no ownership
  • return value explicitly can be NULL (but possible with smart_ptr, too)
  • try to avoid

Smart Pointers

  • Generally a good idea
  • Ideal situation: no explicit deletes in your code

boost::scoped_ptr / scoped_array

  • small, lightweight, non-copyable
  • no transfer of ownership possible (but object can be exchanged via reset)
  • useful in classes for members created by methods that need to be deleted by the destructor
  • useful in functions for objects created on the heap that must be destroyed when going out of scope
  • shared_array: for arrays, provides operator[] and calls delete[]

boost::shared_ptr / shared_array

  • reference counting: multiple objects can own the pointer
  • ideal for transfering ownership, storing in lists, etc.

boost::weak_ptr

  • "reference" to a shared_ptr, but does not own object and will not stop objects from being deleted, but you know if the object still exists.
  • can be converted to shared_ptr using lock()

std::auto_ptr

  • do not use, boost provides better alternatives


Edit | Attach | Refresh | Diffs | More | Revision r1.8 - 22 Dec 2008 - 13:50 - PeterKeitler

Lehrstuhl für Computer Aided Medical Procedures & Augmented Reality    rss.gif