-2
\$\begingroup\$

How can I access a pointer, box in this code, from in a different function, such as this code's update gameloop?

int main()
{
    ScenePrimitive *box = new ScenePrimitive(ScenePrimitive::TYPE_BOX, 1,1,1);
}
int update()
{
 cout box;
}
\$\endgroup\$

closed as off topic by sam hocevar, user744, MichaelHouse, Nicol Bolas, doppelgreener Sep 9 '12 at 8:08

Questions on Game Development Stack Exchange are expected to relate to game development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ This is not really game dev related. \$\endgroup\$ – nathan Sep 8 '12 at 11:39
  • 5
    \$\begingroup\$ I strongly recommend that you find a decent free C++ book like Thinking in C++. This question demonstrates a lack of fundamental language understanding. \$\endgroup\$ – Lars Viklund Sep 8 '12 at 12:18
  • 1
    \$\begingroup\$ -1: Due to lack of research effort. \$\endgroup\$ – Nicol Bolas Sep 8 '12 at 16:45
1
\$\begingroup\$

You could use a global variable for your sceneprimtive:

ScenePrimitive* gBox;

int main()
{
    gBox = new ScenePrimitive(ScenePrimitive::TYPE_BOX, 1,1,1);

    //..
    delete gBox;
}

int update()
{
    cout gBox;
}

OR take the sceneprimitive as an parameter of your update function:

int update(ScenePrimitive* &Box)
{
    cout Box;
}

int main()
{
    ScenePrimitive* Box = new ScenePrimitive(ScenePrimitive::TYPE_BOX, 1,1,1);

    update(Box);

    //..
    delete Box;
}
\$\endgroup\$
  • \$\begingroup\$ I can't use a parameter since update() is the gameloop. The global works fine however. Thanks \$\endgroup\$ – andrew Sep 8 '12 at 15:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.