Can you hold a vector of pointers in the same class as the instances of those objects?

Using SFML, I had a Board class which held multiple vectors of all of my object types in the game, and then it also held a vector of pointers to the memory addresses of these object instances, like this
1
2
3
4
5
6
7
8
9
10
11
class Board{
//...
std::vector<AbstractObject*> GetAllLevelObjects(){ return allLevelObjects; }
//so these are used to hold my object instances for each level
std::vector<ObjectOne> someObjects;
std::vector<ObjectTwo> moreObjects;

/*this is used to easily loop through all my objects and draw them based on if
 they're in the background or wherever*/
std::vector<AbstractObject*> allLevelObjects;
};


When looping through this vector and drawing the sprites of the objects, I get the runtime error 0xC0000005: Access violation reading location 0x00277000.
.
I solved this error by storing the vector of pointers in the class that holds my Board instance, but I'm wondering why only this solution worked? Why couldn't I just have my vector of pointers in the same class that the instances of those objects were in?
0xC0000005 is an invalid memoty access error. Usually it happens when you are trying to dereference invalid pointer. Are you sure that all pointers are valid? That no reallocation.deleting objects took place?
This vector of pointers is going to be both a maintenance nightmare and a performance bottleneck.
To preserve sanity, what would you have to do when one of these vectors reallocates memory?
Consider something like this instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
#include <utility>

struct object
{
    virtual ~object() = default ;
    virtual void paint() const = 0 ;
    // ...
    int n = 0 ;
};

struct object_A : object
{
    virtual void paint() const override { std::cout << "object_A::paint (" << this << ")\n" ; }
    // ...
};

struct object_B : object
{
    virtual void paint() const override { std::cout << "object_B::paint (" << this << ")\n" ; }
    // ...
};

struct board
{
    void paint_all_objects() const { for_each_object( []( const object& obj ) { obj.paint() ; } ) ; }
    void update_all_objects() { for_each_object( []( object& obj ) { ++obj.n ; } ) ; }

    std::vector<object_A> some_objects{4};
    std::vector<object_B> more_objects{3};

    template < typename FN > void for_each_object( FN&& fn )
    {
        for( auto& a : some_objects ) std::forward<FN>(fn)(a) ;
        for( auto& b : more_objects ) std::forward<FN>(fn)(b) ;
    }

    template < typename FN > void for_each_object( FN&& fn ) const
    {
        for( const auto& a : some_objects ) std::forward<FN>(fn)(a) ;
        for( const auto& b : more_objects ) std::forward<FN>(fn)(b) ;
    }
};

int main()
{
    board b ;
    b.update_all_objects() ;
    b.paint_all_objects() ;
}

http://coliru.stacked-crooked.com/a/f8d49dd5008750fc
Topic archived. No new replies allowed.