Page 1 of 1
Variable outside of scope
Posted: Mon Jan 09, 2012 5:04 pm
by coderaider249
Hi, I have my scene initialized in an if statement. but my main loop is outside the if statement, so I can't access the scene node pointers(or any other variable) in that if statement from the main loop. Is there any way in C++ to use variables that are created inside an if statement out of it?
Re: Variable outside of scope
Posted: Mon Jan 09, 2012 5:46 pm
by Radikalizm
You might want to review your design instead of trying to look for ugly workarounds
All variables created within a scope get deleted as soon as that scope is no longer valid, unless they were allocated dynamically on the heap, so such a workaround does not exist
Re: Variable outside of scope
Posted: Mon Jan 09, 2012 6:24 pm
by CuteAlien
You should probably work with classes. Put variables in classes and you can access them anywhere within that class.
Re: Variable outside of scope
Posted: Mon Jan 09, 2012 8:59 pm
by hybrid
Well, wouldn't call it workaround, but common practice: Create the pointer before and init to zero. Then just assign the pointers in your if statement. But as CuteAlien said, it's probably better to give everything a little more structure.
Re: Variable outside of scope
Posted: Tue Jan 10, 2012 10:54 am
by coderaider249
yeah, I tried with creating pointers in the header file although it took the red underlines off but got errors in compile. maybe I want to review the code again.
@CuteAlien: I think you meant the same thing, right?
thanks!
Re: Variable outside of scope
Posted: Tue Jan 10, 2012 11:05 am
by Radikalizm
coderaider249 wrote:yeah, I tried with creating pointers in the header file although it took the red underlines off but got errors in compile. maybe I want to review the code again.
@CuteAlien: I think you meant the same thing, right?
thanks!
Using classes is different from using globals

Maybe you should review your C++ skills before you go on?
Re: Variable outside of scope
Posted: Tue Jan 10, 2012 11:10 am
by coderaider249
Radikalizm wrote:coderaider249 wrote:yeah, I tried with creating pointers in the header file although it took the red underlines off but got errors in compile. maybe I want to review the code again.
@CuteAlien: I think you meant the same thing, right?
thanks!
Maybe you should review your C++ skills before you go on?
definitely!
