I heard pointers are not recommended in C++ but I don’t understand why.
Pointers are a lot harder to work with than say, regular objects. It's not that they are completely bad, it's just that there are just so many better options in almost every use case for pointers.
So for instance, say you have a function parameter you'd like to modify in the caller function. In C, you'd use a pointer:
void func(int* someMumber) {
*somenumber = 3; // modifies value in caller
}
In C++, this is replaced with passing by reference:
void func(int& someMumber) {
somenumber = 3; // modifies value in caller
}
This way, there is no messy syntax to deal with, and no fear of getting passed a bad pointer, etc. This should always work no matter what the caller passes us.^1
Another example is let's say you want dynamically allocated data:
int* dynamicArray = new int[size];
When you're done with it, you will have to remember to delete
it:
delete [] dynamicArray;
This can get messy, and then you have to keep track of what you did and didn't delete. And then if you don't delete something, that creates a memory leak.
C++ has a much better solution for this: std::vector
. Then I can add as many elements to the array the instant I need them:
std::vector<int> dynamicArray;
// later, when I need to store a value
dynamicArray.push_back(someValue);
No worrying about leaked data, or anything like that. It will all just deallocate automatically when it's done.
Need a pointer anyway for other some reason? Try a smart pointer:
std::unique_ptr<int> ptr = new int;
Smart pointers deallocate data automatically without you have to worry about it. It just makes your life easier. Plus, you can convert these to "raw" pointers whenever you need to.
So it's not that they are bad, it's just they are made obsolete by other things. Thus, they are not recommended.
Naturally, for better performance, I should go with a vector of a pointer of class objects?
The compiler will optimize a lot for you, so there isn't a lot of need to worry about this. On the other hand, using pointers will make things a lot harder on yourself. You might want to consider one of the above options instead.
Or maybe is it possible to create a vector of reference of class objects?
Not sure if this is directly possible, but it might be possible with a std::reference_wrapper
.
So my questions are :
What is the difference between these ways?
What is the most optimized to create a vector of class objects?
The biggest difference is that if you push the address of a local variable to your pointer vector, and it goes out of scope, you vector will be filled with garbage. To do this properly, you will need to allocate a new object. Then you will have to delete it. It's just a lot more work.
If you use a vector of references, you will run into the same types of scope problems that using a local variable address will create.
If you store it by value, you won't have to worry about any of this.
Again, don't worry about optimizations. The compiler will figure that out for you. If you are trying to do this as an optimization, you are just making more work for yourself.
1: Actually, the caller can't pass literals to us, because non-const references can't bind to literals. But that is beside the point here.