Pointers,References,Smart Pointers

Hey,

I'm a Game Developer and saw a lot of Game Engine Code and Company Code(C++)
and I'm still not sure about References, Pointers and Smart Pointers.

I have following Problems:

1)
I've read a lot of books since I started with C++(years ago) and it was always said to use References over Pointers, but when I look through the code of Engines or Frameworks I nearly never see any Reference , but normal Pointers(parameters, return values, etc...). For an example,in engines i nearly always get a pointer to Object which is inside my Game(e.g. a Actor). I always just return Objects as Reference (e.g Object& GetObject(){return obj;} ) Can anyone help me here?

2)
Before I started with C++, I've learned Java & C#, where you created every Object with Object obj = new Object(); In C++ you shouldn't just use new, but you should use a smart pointer or no pointer at all.

In other forums, people told me that I actually nearly never need to create Objects with new at all in C++, but I'm not quite sure about this answer. When should I actually use smart pointers, because unit now everything worked without them, even without any dynamically created object(new), but this can't be the right way to put everything on the stack.(Performance,other improvements?)

3)

I also see a lot of Lists/Arrays in Engines with pointers,

e.g. Array<Actor*> .......

But I always create them without pointers, Array<Object> .
And as parameter and getter I would also just use them with references:

1
2
3
Array<Object>& GetArray(){return myArray}...

void function(Array<Object>& myArray)


(This is actually an question about lists/vectors/arrays in general and if there performance issues with the way i'm using them)

Can anyone help me?
Last edited on
2)
because until now everything worked without them


Of course 'raw' pointers have always worked, but with those it's up to you as a developer to free up this memory once you have used it. This isn't a problem when your problem is only 10 lines long and you're the only person working on it, but imagine huge software projects with many developers on it. The chances are this freeing up of memory will be overlooked from time to time.
It's best to use smart pointers because you don't have to worry about this at all. They 'know' when to free up the memory, giving the developer one less thing to worry about.
Last edited on
Pointers vs references:
[smart]Pointers can be reboung to point to other object, references cannot.
Pointers can have "null value"
Reference are less prone to errors.

I say take and return parameters by reference unless you have compelling reason to do differently. For example some Factory would have to return [smart]pointer, because nobody expects owning reference. Not to mention that reference would make destroying object not that simple.

people told me that I actually nearly never need to create Objects with new at all in C++
They mean that there is many functions which do so for you. Like make_xxx line:
1
2
auto night_sky = std::make_unique<Skybox>(textures[sky_night], gen_sk_size);
auto parser = std::make_shared<Parser>(new_ruleset);


Also [smart]pointers are needed when you want to create a container of polymorfic types (containig both derived and base classes)
Additionally if your classes are mostly data and they are expensive to even move, storing [smart]pointers to them improves perfomance.
Last edited on
@mutexe

Well when I use pointers i just use them like -> string name* = &otherStr;
No dynamic allocation or anything.


@MiiniPa

What do you mean with:

"some Factory would have to return [smart]pointer, because nobody expects owning reference. Not to mention that reference would make destroying object not that simple."
?

and

"Also [smart]pointers are needed when you want to create a container of polymorfic types (containig both derived and base classes)"



"Additionally if your classes are mostly data and they are expensive to even move, storing [smart]pointers to them improves perfomance."

this sound very interesting until now I always just create class objects on the stack (Object obj;) can you tell me more about the?






Last edited on
What do you mean with

Containers with pointers to polimorfic objects:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <vector>
#include <iostream>
#include <memory>

struct foo
{
    virtual void speak() const {std::cout << "Hi!\n";}
    virtual ~foo() = default;
};

struct bar: foo
{
    virtual void speak() const override {std::cout << "What do you want?\n";}
};


int main()
{
    std::vector<std::unique_ptr<foo>> baz;
    baz.push_back(std::make_unique<foo>());
    baz.push_back(std::make_unique<bar>());
    for(const auto& it: baz)
        it -> speak();
}
Hi!
What do you want?
It is impossible to do with container of plain objects.

Also I used Factory Pattern as example: http://www.oodesign.com/factory-pattern.html
It can return object of different types depending on input parameters. Only viable awy to return a polymorfic object is by pointer.

Look at what happens if we want to use a reference:
1
2
3
int& x = *(new int);
//.. use x
delete &x;

Well when I use pointers i just use them like -> string name* = &otherStr;


and this is the danger with raw pointers. Have a read of this:

http://stackoverflow.com/questions/417481/pointers-smart-pointers-or-shared-pointers

Topic archived. No new replies allowed.