Muthu
Muthu

Reputation: 105

Pointer as member or Reference as member

I need one clarification in c++ linux.

I have class C1 and another class C2. C1 will have the reference of C2.

class C1
{

   C2 &obj ;

}

i am thinking of two choices here ,

  1. Directly holding the reference of C2 as C2 &obj;
  2. Creating the pointer of C2, as c2* obj;

Which is good ? what is the difference in it ? when choose either?

Upvotes: 5

Views: 704

Answers (4)

Lundin
Lundin

Reputation: 213513

Neither is good. If you have any pointer or reference member variable pointing at something allocated outside your class, it most likely means that the program design is flawed somehow.

One case I can come up with where it is justified, is when you have some sort of peculiar, external garbage collector functionality. (For example C++ Builder's automatic allocation/deallocation of VCL components on the heap.)

Another case is where it could be valid, is when you are emulating a C struct with a C++ class, or when writing a private class inside another class.

But then most likely, the need to point at something outside the class comes from flawed program design. I would think twice of why my code needs to do this, and at the very least try to add as much const correctness at possible to the pointer, ie const *c2 const obj;

If you have peculiar member pointers like this, you must also most likely manually write a copy constructor, assignment operator and destructor for your class.

Edit: The rationale for why this is bad practice is the concept called private encapsulation, which is one of the cornerstones of the program design concept called object-oriented design.

Upvotes: 0

BЈовић
BЈовић

Reputation: 64213

Which one you are going to use, depends on what you exactly need.

For example, you can use a reference member variable if you need some kind of dependency injection. Even then, you can use pointers. However, keep in mind that classes with reference members should generally be non-copyable.

In most cases, you should either use pointers or value member variables.

Upvotes: 0

D-rk
D-rk

Reputation: 5919

a pointer has two main advantages to references. the pointer can be null and a pointer can be moved (with ++ operator for example). The moving doesn't help you here. If your class C2 is not known when you instantiate C1 then you should use a pointer because you have no way to initialize the reference, but the pointer can be initialized to null.

Like Als said, i would also recommend using pointers when the C2 instance may change. You can uses references in this case but i think pointers are a 'cleaner' solution.

Upvotes: 0

Alok Save
Alok Save

Reputation: 206518

Avoid using a reference member as much as possible.

The same differences as that of references and pointers apply here,
If you have a reference member then it must be initialized at the time of creation of your class object you cannot have a lazy initialization as in case of pointer member because references cannot be NULL and cannot be reseated, while a pointer member can be made to point to a C2 instance lazily as and when required.

Also, note that there are other side effects as well, Once you have a reference member the compiler will not generate the copy assignment operator(=) & You will have to provide one yourself, It is cubersome to determine what action your = operator shall take in such a case.

For most practical purposes(unless you are really concerned of high memory usage due to C2 size) just holding an instance of C2 as member of C1 should suffice, instead of pointer or reference member, that saves you a whole lot of worrying about other problems which reference/pointer members bring along though at expense of extra memory usage.

If you must, use a pointer make sure you use a smart pointer instead of a raw pointer, that would make your life much easier with pointers.

Upvotes: 2

Related Questions