Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer vs variable in class

I know what is the difference and how they both work but this question is more about coding style.

Whenever I'm coding I make many classes, they all have variables and some of them are pointers and some are normal variables. I usually prefer variables to pointers if that members lasts as long as the class does but then my code becomes like this:

engine->camera.somevar->x;
// vs
engine->camera->somevar->x;

I don't like the dot in the middle. Or with private variables:

foo_.getName();
// vs
foo_->gatName();

I think that dot "disappears" in a long code. I find -> easier to read in some cases.

My question would be if you use pointers even if the variable is going to be created in the constructor and deleted in the destructor? Is there any style advice in this case?

P.S. I do think that dot is looks better in some cases.

like image 592
Pijusn Avatar asked Jan 14 '11 09:01

Pijusn


2 Answers

First of all it is bad form to expose member variables.

Second your class should probably never container pointers.

Slight corolary: Classes that contain business logic should never have pointers (as this means they also contain pointer management code and pointer management code should be left to classes that have no business logic but are designed specifically for the purpose of managing pointers (smart pointers and containers).

Pointer management classes (smart pointers/containers) should be designed to manage a single pointer. Managing more than one is much more difficult than you expect and I have yet to find a situation where the extra complexity paid off.

Finally public members should not expose the underlying implementation (you should not provide access to members even via getters/setters). This binds the interface to tightly to the implementation. Instead your public interface should provide a set of actions that can be performed on the object. i.e. methods are verbs.

In C++ it is rare to see pointers.
They are generally hidden inside other classes. But you should get used to using a mixture of -> and . as it all depends on context and what you are trying to convey. As long as the code is clean and readable it does not matter too much.

A personal addendum:

I hate the _ at then end of your identifier it makes the . disapear foo_.getName() I think it would look a lot better as foo.getName()

like image 129
Martin York Avatar answered Oct 31 '22 08:10

Martin York


If the "embedded" struct has exactly the same lifetime as the "parent" struct and it is not referenced anywhere else, I prefer to have it as a member, rather than use a pointer. The produced code is slightly more efficient, since it saves a number of calls to the memory allocator and it avoids a number of pointer dereferences.

It is also easier to handle, since the chance of pointer-related mistakes is reduced.

If, on the other hand, there is the slightest chance that the embedded structure may be referenced somewhere else I prefer to use a separate struct and pointers. That way I won't have to refactor my code if it turns out that the embedded struct needs to be pulled out from its parent.

EDIT:

I guess that means that I usually go with the pointer alternative :-)

EDIT 2:

And yes, my answer is assuming that you really want (or have) to chose between the two i.e. that you write C-style code. The proper object-oriented way to access class members is through get/set functions.

My comments regarding whether to include an actual class instance or a pointer/reference to one are probably still valid, however.

like image 22
thkala Avatar answered Oct 31 '22 07:10

thkala