0

I have a list where I store a pointer to a pointer of objects. I have a function where I create pointers to these objects and store their addresses in the list (hence pointer to a pointer). However once this function is done that pointer is no longer valid (the object is but not the pointer because it's out of scope) so now my pointer to pointer doesn't work. How do you get around this?

list<Actor**> lst;
void CreateEnemy()
{
    Actor* a = new Actor();

    lst.push_back(&a);
}

int _tmain(int argc, _TCHAR* argv[])
{
    CreateEnemy();
    // at this point the pointer to a pointer stored in lst isn't valid anymore because it went out of scope after CreateEnemy() completed.
}
3
  • 4
    Is there a particular reason to store a pointer to a pointer to the object in the list instead of a pointer to the object directly? Commented Sep 8, 2012 at 15:46
  • If you need (please make sure you do) the pointers to pointers, I'd think you'd have to allocate an Actor ** in your function, not just an Actor *. Commented Sep 8, 2012 at 15:46
  • chris how would that look? I can't get the syntax right. So Actors get created in one class and stored in this list. They get passed to Projectiles class as the projectiles target. But 5 projectiles could have the same target. The first projectile to hit the target will kill the Actor causing it to get deleted (which I want), but the other 4 projectiles will still have pointers to the now deleted target and a NULL check won't work unless I'm checking the pointer to the pointer. Commented Sep 8, 2012 at 15:52

3 Answers 3

2

Here's a simple example of something you could do based on your comments.

list<unique_ptr<Actor>> lst;

void CreateEnemy()
{
    lst.push_back(unique_ptr<Actor>(new Actor));
}

void KillRandomEnemies()
{
    for (auto& i : lst)
    {
        if (rand() % 100)
            i.reset();
    }
}    

class Projectile
{
public:
    Projectile(unique_ptr<Actor> & a)
        :actor(&a)
    {}

    bool IsTargetDead() const { return !(*actor); }

private:
    unique_ptr<Actor> * actor;
};

int main()
{
    CreateEnemy();
    Projectile proj(lst.front());

    while (something())
    {
        KillRandomEnemies();

        if (proj.IsTargetDead())
            whatever();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

what do I need with the for loop inside KillRandomEnemies()? It doesn't like that syntax and I've never seen it before. Guessing it's the new C++ style? I'm still in oldville with C++.
@user441521: Yes, it's a range-based for loop, new in C++11. If your compiler doesn't support it, just replace it with a for loop that iterates over every element in the container. It's been supported in GCC for a while now. Visual C++ 10 doesn't support it, but I think version 11 does.
1

Just use std::list<Actor*> and store the pointer. Or, even better, std::list<Actor>. There's nothing here that suggests a need for any pointers, much less a pointer to a pointer.

16 Comments

If he does need a pointer (perhaps for polymorphism), it should be a smart pointer.
@BenjaminLindley - not necessarily. A smart pointer is only appropriate when ownership is shared.
No, a smart pointer is appropriate with any kind of ownership. shared_ptr for shared ownership, and unique_ptr for non-shared ownership. A non-smart pointer is only appropriate as a non-owning reference. That's not likely the case here, since he's allocating the object dynamically.
There is polymorphism, and like Pete says I don't want to really share this. When the actor is dead I want it deleted, but many other classes may have references to it. I want to be able to check against NULL in those classes against the references but I for sure want that actor deleted when it's dead.
@BenjaminLindley - well, there are different perspectives there. A smart pointer means not having to destroy the managed objects in the manager's destructor, but it also implies sharing.
|
0

Try this : after the call to createEnemy :

Actor * a = * lst.front();
a -> some function.

Is this still doing problems?

I tested the code on my own class and it was fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.