C ++ Iterating over class pointers using std :: vector

I am trying to Iterate over a vector using pointers. I have a vector:

 std::vector<GameObject*> objects;

and loads of such functions:

void Game::update()
 {
    std::vector<GameObject*>::iterator itr;
    for( itr = objects.begin();itr < objects.end();++itr)
    {
        itr->update();//I need to call a abstract function in the GameObject Class
    }
 }
 Game::~Game()
 {

    delete ball;
    delete player;
 }
Game::Game()
{
    ball = new GOBall(800/2 - GOBall::SIZE/2,600/2 - GOBall::SIZE/2);
    player = new GOPlayer(0, 600/2 - GOPlayer::SIZEY/2,ball);
    objects.push_back(ball);
    objects.push_back(player);
}

As you can see, I am trying to iterate in such a way that it still allows me to call the function, as well as parse the polymorphic class in other polymorphic classes (therefore, the reason it is declared before being parsed into a vector), which I keep getting is an error :

C2839: Invalid return type 'GameObject * const *' for overloaded 'statement →'

and error:

C2039: 'update': is not a member of 'std :: _ Vector_const_iterator <_Ty, _Alloc>'

which tells me that I cannot call ball->update()either player->update()through an iterator, so how to do this?

+4
4

(*itr)->update();

:

GameObject* pGameObject = *itr;
pGameObject->update();
+6

++ 11:

for (GameObject* gameObject : objects) {
    gameObject->update();
}
+9

We can use the cycle-based range one step better:

for (auto& game_object : objects)
  game_object->update();
+3
source

A more complicated way is to create your own iterator adapter. Here is an example (the full example will compile and run):

#include <iostream>

#include <vector>
#include <memory>

struct GameObject {
    GameObject(int id)
    : _id { id }
    {}

    virtual void fireLazorz() {
        std::cout << "Game Object " << _id
        << ": I'm a-firin' mah lazorz!" << std::endl;
    }
private:
    int _id;
};


using vec_t = std::vector<std::unique_ptr<GameObject>>;

struct gameobject_deref_iterator : public vec_t::const_iterator
{
    using parent_t = vec_t::const_iterator;

    gameobject_deref_iterator(parent_t src)
    : parent_t(std::move(src))
    {

    }

    // override the indirection operator
    GameObject& operator*() const {
        return *(parent_t::operator*());
    }

    GameObject* operator->() const {
        return parent_t::operator->()->get();
    }

};
using namespace std;

int main()
{
    vec_t gameObjects;

    for(int i = 0 ; i < 10 ; ++i) {
        gameObjects.emplace_back(
                                 new GameObject{ i }
                                 );
    }

    // now iterate the game objects, starting with the 5th one

    gameobject_deref_iterator first { next(begin(gameObjects), 5) };
    gameobject_deref_iterator last { end(gameObjects) };
    cout << "We have " << last - first << " GameObjects:" << endl;
    for(auto it = first ; it != last ; ++it) {
        it->fireLazorz();
    }

    return 0;
}

output:

Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1

Executing the program....
$demo 
We have 5 GameObjects:
Game Object 5: I'm a firin my lazorz!
Game Object 6: I'm a firin my lazorz!
Game Object 7: I'm a firin my lazorz!
Game Object 8: I'm a firin my lazorz!
Game Object 9: I'm a firin my lazorz!
0
source

Source: https://habr.com/ru/post/1541279/


All Articles