Sign in to follow this  
Makkedah

Global pointers to objects?

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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this