When to use pointers in C ++

I just started learning pointers in C ++, and I'm not very sure when to use pointers, and when to use real objects.

For example, in one of my assignments, we must build the gPolyline class, where each point is defined by gVector. Right now, my variables for the gPolyline class look like this:

private: vector<gVector3*> points; 

If I had a vector <gVector3> instead, what's the difference? Also, is there a general rule when to use pointers? Thanks in advance!

+6
c ++ pointers vector
source share
5 answers

The general rule is to use pointers when you need, and values ​​or references when you can.

If you use vector<gVector3> , the inserted elements will copy these elements and the elements will no longer be associated with the element you pasted. When you store pointers, the vector simply refers to the object you inserted.

So, if you want several vectors to have the same elements, so that changes in the element are reflected in all vectors, you need vectors containing pointers. If you do not need such functions, storing values ​​is usually better, for example, this saves you from worrying about when to delete all specified objects.

+2
source share

Pointers should generally be avoided in modern C ++. The main purpose of pointers at present is that pointers can be polymorphic, while explicit objects are not.

If you need polymorphism at present, although it is better to use a class of smart pointers - for example, std::shared_ptr (if your compiler supports C ++ 0x extensions), std::tr1::shared_ptr (if your compiler does not support C ++ 0x but supports TR1) or boost::shared_ptr .

+1
source share

It is generally recommended that you use pointers when you need to, but references or, alternatively, object objects (think about values) when you can.

First you need to know if gVector3 meets the requirements of standard containers, namely if gVector3 can be copied and assigned. This is useful if gVector3 is also constructive by default (see UPDATE Note below). Assuming this is the case, you have two options: store gVector3 objects directly in std::vector

 std::vector<gVector3> points; points.push_back(gVector(1, 2, 3)); // std::vector will make a copy of passed object 

or manage the creation (as well as destruction) of gVector3 objects manually.

std :: vector points; points.push_back (new gVector3 (1, 2, 3)); // ...

When the points array is no longer needed, remember to talk about all the elements and call the delete operator on it.

Now, it is your choice if you can manipulate gVector3 as objects (you can assume that you consider them to be values ​​or objects), because (if you see the condition above) due to the availability of the copy constructor and assignment operator, operations are possible:

 gVector3 v1(1, 2, 3); gVector3 v2; v2 = v1; // assignment gVector3 v3(v2); // copy construction 

or you may need or need to allocate gVector3 objects in dynamic storage using the new operator. So, you may need or need to manage the lifetime of these objects yourself.

By the way, you might also be interested in When to use links and when to use pointers?

UPDATE: This is an explanation of the default design note. Thanks to Neil for being incomprehensible. As Neil correctly noted, this is not required by the C ++ standard, however, I pointed to this function because it is important and useful. If type T not constructive by default, which is not required by the C ++ standard, then the user should be aware of potential problems that I am trying to illustrate below:

 #include <vector> struct T { int i; T(int i) : i(i) {} }; int main() { // Request vector of 10 elements std::vector<T> v(10); // Compilation error about missing T::T() function/ctor } 
+1
source share

You can use pointers or objects - this is really the same at the end of the day.

If you have a pointer, you will need to allocate space for the actual object (then point to it) in any way. At the end of the day, if you have a million objects, regardless of whether you save the pointers or the objects themselves, you will have space for the million objects allocated in memory.

When to use pointers instead? If you need to transfer objects yourself, change individual elements after they are in the data structure without receiving them each time, or if you use a specialized memory manager to control the distribution, deallocation and cleaning of objects.

Putting objects in an STL structure is simpler and simpler. This requires fewer * and β†’ operators, which, in your opinion, are difficult to understand. Some STL objects will need to have the objects themselves instead of pointers in their default format (i.e., hash tables that must have a hash entry, and you want a hash object, not a pointer to it), but you can always get around these are overriding functions, etc.

Bottom line: use pointers when it makes sense. Use objects otherwise.

0
source share

Usually you use objects.
It’s easier to eat an apple than an apple on a stick (OK 2 meters, because I love candy apples).

In this case, just make it a vector <gVector3>

If you have a vector <g3Vector *> this means that you dynamically select new g3Vector objects (using the new operator). If so, then you need to call delete on these pointers at some point, and std :: Vector is not intended for this.

But every rule is an exception.

If g3Vector is a huge object that is worth a lot of copying (it's hard to say, read your documentation), then it can be more efficient to store as a pointer. But in this case, I would use boost :: ptr_vector <g3Vector> since this automatically controls the lifespan of the object.

0
source share

All Articles