Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - when should I use a pointer member in a class

Tags:

c++

pointers

One of the thing that has been confusing for me while learning C++ (and Direct3D, but that some time ago) is when you should use a pointer member in a class. For example, I can use a non-pointer declaration:

private:
    SomeClass instance_;

Or I could use a pointer declaration

private:
   Someclass * instance_

And then use new() on it in the constructor.

I understand that if SomeClass could be derived from another class, a COM object or is an ABC then it should be a pointer. Are there any other guidelines that I should be aware of?

like image 765
Extrakun Avatar asked Jul 24 '09 03:07

Extrakun


People also ask

When should a pointer be used?

Pointers are used with data structures. They are useful for representing two-dimensional and multi-dimensional arrays. An array, of any type, can be accessed with the help of pointers, without considering its subscript range. Pointers are used for file handling.

Can pointers be class members?

Pointers to members allow you to refer to nonstatic members of class objects. You cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object. To point to a static class member, you must use a normal pointer.

Is it perfectly valid to create pointers that point to classes?

Pointers to classesIt is perfectly valid to create pointers that point to classes. We simply have to consider that once declared, a class becomes a valid type, so we can use the class name as the type for the pointer.

What kind of copy should be made when attributes of a class are pointers?

The copy constructor should copy the values of all non-pointer data members, and should copy the objects pointed to by all pointer data members (this is sometimes called a deep copy).


2 Answers

A pointer has following advantages:

a) You can do a lazy initialization, that means to init / create the object only short before the first real usage.

b) The design: if you use pointers for members of an external class type, you can place a forward declaration above your class and thus don't need to include the headers of that types in your header - instead of that you include the third party headers in your .cpp - that has the advantage to reduce the compile time and prevents side effects by including too many other headers.

class ExtCamera;  // forward declaration to external class type in "ExtCamera.h"  class MyCamera { public:    MyCamera() : m_pCamera(0) { }    void init(const ExtCamera &cam);  private:    ExtCamera  *m_pCamera;   // do not use it in inline code inside header! }; 

c) A pointer can be deleted anytime - so you have more control about the livetime and can re-create an object - for example in case of a failure.

like image 198
3DH Avatar answered Oct 20 '22 08:10

3DH


The advantages of using a pointer are outlined by 3DH: lazy initialization, reduction in header dependencies, and control over the lifetime of the object.

The are also disadvantages. When you have a pointer data member, you probably have to write your own copy constructor and assignment operator, to make sure that a copy of the object is created properly. Of course, you also must remember to delete the object in the destructor. Also, if you add a pointer data member to an existing class, you must remember to update the copy constructor and operator=. In short, having a pointer data member is more work for you.

Another disadvantage is really the flip side of the control over the lifetime of the object pointed to by the pointer. Non-pointer data members are destroyed automagically when the object is destroyed, meaning that you can always be sure that they exist as long as the object exists. With the pointer, you have to check for it being nullptr, meaning also that you have to make sure to set it to nullptr whenever it doesn't point to anything. Having to deal with all this may easily lead to bugs.

Finally, accessing non-pointer members is likely to be faster, because they are contiguous in memory. On the other hand, accessing pointer data member pointing to an object allocated on the heap is likely to cause a cache miss, making it slower.

There is no single answer to your question. You have to look at your design, and decide whether the advantages of pointer data members outweigh the additional headache. If reducing compile time and header dependencies is important, use the pimpl idiom. If your data member may not be necessary for your object in certain cases, use a pointer, and allocate it when needed. If these do not sound like compelling reasons, and you do not want to do extra work, then do not use a pointer.

If lazy initialization and the reduction of header dependencies are important, then you should first consider using a smart pointer, like std::unique_ptr or std::shared_ptr, instead of a raw pointer. Smart pointers save you from many of the headaches of using raw pointers described above.

Of course, there are still caveats. std::unique_ptr cleans up after itself, so you do not need to add or modify the destructor of your class. However, it is non-copiable, so having a unique pointer as a data member makes your class non-copiable as well.

With std::shared_ptr, you do not have to worry about the destructor or copying or assignment. However, the shared pointer incurs a performance penalty for reference counting.

like image 40
Dima Avatar answered Oct 20 '22 10:10

Dima