• Advertisement
Sign in to follow this  

Global pointers to objects?

This topic is 4360 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making an extremely basic RPG in windows. Basically what I decided that I needed to do was find a way to make the main character data global. I assumed that this would mean having a global pointer, but I don't know where to put this global pointer, or if it is even possible to make a global pointer to an object. I'm certainly not convicted that this is the right way to do it, so if there are any better suggestions for how to make methods from several different classes access data from the same object, I am quite open to them.

Share this post


Link to post
Share on other sites
Advertisement
Personally, I'd have the objects set up so that when you create them, you pass the object a pointer to Player, and have the object remember it, so that it can be used from that object.

A quick example:



class Player
{
public:

void sayStuff() { say("Hi there"); }

};

class myObject
{
public:

Player* m_pPlayer;
myObject(Player* player_ptr) { m_pPlayer = player_ptr; }
void makePlayerSayStuff() { m_pPlayer->sayStuff(); }

}

Player *bob = new Player;
myObject foo(bob);
foo.makePlayerSayStuff();


Share this post


Link to post
Share on other sites
It would also be useful to use boosts shared_ptr to help manage then lifetime of the object.

Dave

Share this post


Link to post
Share on other sites
Sorry, I'm really quite a noob as far as pointers and objects are concerned. Could this be explained a little more in detail? I don't really understand any of the part under the class myObject, and I don't know anything about a shared_ptr (other than the obvious).

Share this post


Link to post
Share on other sites
Hey bud,

What is being done in that code is fairly simple, but slightly unusual. He is saying that wrapping then pointer with a static object, useful for 1 thing:

It wraps the pointer and can therefore manage it's lifetime. It lives as long as the object does, that is if you clean it up in the destructor.

You are also providing an interface for the pointer.

Anything else? I hope that helps,

Dave

Share this post


Link to post
Share on other sites
Sorry, I'm still clueless. What is a wrapper?

And I tried integrating the example code, using my own classes, objects, and pointer names, with my current code to no avail.

Let's see if I can expound further. I have a class called Character, and I want to make a pointer to an object that I create with this class, an object whose methods will be accessible to all the files of the project.

Share this post


Link to post
Share on other sites
A global pointer is a pointer declared outside a structure, class or function.

CPlayer *pPlayerA;

CPlayer *GetMainPlayer()
{
return pPlayerA;
}

struct Legion
{
CPlayer *pPlayerB;
...
};
void foo( void )
{
pPlayer *pPlayerC = GetMainPlayer();
...
}

Player A is the only global pointer in this exe or dll. Player B is in a structure and would have to be referenced through an instance of Legion. Player C is in a function and would only referenced in foo and valid until foo exited. GetMainPlayer is a function wrapper where is allows you access to PlayerA by returning a pointer to Player A.
Example of a wrapper on CodeProject.
http://www.codeproject.com/cpp/Functor.asp?print=true

Search for more information "wrapper pointer C++".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement