Jump to content
  • Advertisement
Sign in to follow this  
Lonefox1

declaring globals in code blocks?

This topic is 4651 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

Hi guys im trying to declare a class instance in one block of code i then need to use this instance in another block of code, how would i go about making it global? language is C++ cheers si

Share this post


Link to post
Share on other sites
Advertisement
Why not just declare it at the top of the file (in the global scope) or in a common scope.


class foo {public: int y};

foo x;

void func1()
{
x.y = 10;
}

void func2()
{
z = x.y;
}


I don't think that you can declare it as a global from within the code block.

Share this post


Link to post
Share on other sites
the instances are created dependant on some values im reading in from a file so i cant really just declare them up top as i dont know how many im gona need :S

Share this post


Link to post
Share on other sites
declare a pointer at the top of the file to the object and then when you create the object save it's address in the pointer.



foo * ptr;

func1()
{
//Create your object however you want
ptr=new foo(...);
}

func2()
{
//You can access the object in here
ptr->x = 10
//...
}

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!