Pointer versus variable in class

I know what the difference is and how they work, but this question is more related to the coding style.

Whenever I code, I make many classes, all of them have variables, and some of them are pointers, and some are ordinary variables. I usually prefer variables to pointers if these members are preserved as long as the class does, but then my code will look like this:

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

I do not like the point in the middle. Or with private variables:

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

I think the dot "disappears" in the long code. I find β†’ easier to read in some cases.

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

PS I think that in some cases the point looks better.

+6
c ++ variables pointers coding-style
source share
4 answers

First of all, this is a bad form for exposing member variables.

Secondly, your class should probably never specify containers.

Minor relationship: classes containing business logic should never have pointers (since this means that they also contain a pointer control code, and the pointer control code should be left to classes that do not have business logic, but are designed specifically for the purpose of pointer management (smart pointers and containers).

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

Finally, public members should not disclose the underlying implementation (you should not provide access to members even through getters / seters). This strongly binds the interface to the implementation. Instead, your public interface should provide a set of actions that can be performed on the object. i.e. verbs methods.

Pointers are rarely found in C ++.
They are usually hidden inside other classes. But you should get used to using the mixture -> and . because it all depends on the context and what you are trying to convey. As long as the code is clean and readable, it doesn't really matter.

Personal application:

I hate _ at the end of your id, which it does. disapear foo_.getName() I think it would look much better than foo.getName()

+5
source share

You should not make your choice because it is easier for you to read "->" :) Using a member variable is usually better, since you cannot make errors with a pointer.

This suggests that using a member variable forces you to reveal your implementation, so you need to use links. But then you must initialize then in your constructor, which is not always possible ...

The solution is to use std :: auto_ptr or boost :: scoped_ptr from a similar smart pointer. There you will get the advantage of both solutions, with very few flaws.

my2c

EDIT:

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

+1
source share

If the "embedded" structure has exactly the same lifetime as the "parent" structure, and it is not mentioned anywhere, I prefer to have it as a member rather than use a pointer. The resulting code is somewhat more efficient because it saves several calls to the memory allocator and avoids a number of pointer markups.

It is easier to deal with, since the likelihood of errors associated with pointers is reduced.

If, on the other hand, there is the slightest likelihood that an inline structure can be indicated somewhere else, I prefer to use a separate structure and pointers. This way, I don’t have to reorganize my code if it turns out that the inline structure should be pulled from the parent element.

EDIT:

I assume this means that I usually use a pointer alternative :-)

EDIT 2:

And yes, my answer suggests that you really want (or have) a choice between the two, that is, you are writing C style code. The right object-oriented way to access class members is through the get / set functions.

My comments on whether to include the actual instance of the class or a pointer / reference to it are probably still valid.

0
source share

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

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

If none of them embarrass you, I would say that all that remains is a matter of personal preference.

0
source share

All Articles