Static Fields

Hi there,

Is it better to declare static fields of a class as a pointer to an object or as a value? And is there any benefit to either one?

1
2
const static Foo Bar;    // value field
const static Foo* Bar;   // pointer field 


As always, thank you for taking the time to answer this.

Regards,

Phil.
It depends whether you want an object or a pointer.


Advantages to using a pointer:
- you can control when the ctor and dtor are called (by explicily calling new/delete)
- or you can point to an object that exists elsewhere (useful if you are not the owner of this object or if ownership is shared)

Disadvantages to using a pointer:
- you have to explicitly call new/delete
- or you have to assign the pointer to point to another object
Topic archived. No new replies allowed.