Pijusn
Pijusn

Reputation: 11293

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.

Upvotes: 8

Views: 3556

Answers (4)

Loki Astari
Loki Astari

Reputation: 264501

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()

Upvotes: 7

neuro
neuro

Reputation: 15180

You should not make your choice because you find '->' easier to read :) Using a member variable is usually better as you can not make mistakes with you pointer.

This said, using a member variable force you to expose your implementation, thus you have to use references. But then you have to initialize then in your constructor, which is not always possible ...

A solution is to use std::auto_ptr or boost::scoped_ptr ot similar smart pointer. There you will get advantage of both solution, with very little drawbacks.

my2c

EDIT:

Some useful links :
Article on std::auto_ptr
boost::scoped_ptr
Pimpl : private implementation

Upvotes: 1

thkala
thkala

Reputation: 86353

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.

Upvotes: 1

Kricket
Kricket

Reputation: 4179

Ideally, you shouldn't use either: you should use getter/setter methods. The performance hit is minimal (the compiler will probably optimize it away, anyway).

The second consideration is that using pointers is a generally dangerous idea, because at some point you're likely to screw it up.

If neither of these faze you, then I'd say all that's left is a matter of personal preference.

Upvotes: 0

Related Questions