Variable outside of scope
-
- Posts: 9
- Joined: Fri Nov 04, 2011 4:38 pm
Variable outside of scope
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?
-
- Posts: 1215
- Joined: Tue Jan 09, 2007 7:03 pm
- Location: Leuven, Belgium
Re: Variable outside of scope
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
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
You should probably work with classes. Put variables in classes and you can access them anywhere within that class.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Admin
- Posts: 14143
- Joined: Wed Apr 19, 2006 9:20 pm
- Location: Oldenburg(Oldb), Germany
- Contact:
Re: Variable outside of scope
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.
-
- Posts: 9
- Joined: Fri Nov 04, 2011 4:38 pm
Re: Variable outside of scope
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!
@CuteAlien: I think you meant the same thing, right?
thanks!
-
- Posts: 1215
- Joined: Tue Jan 09, 2007 7:03 pm
- Location: Leuven, Belgium
Re: Variable outside of scope
Using classes is different from using globals Maybe you should review your C++ skills before you go on?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!
-
- Posts: 9
- Joined: Fri Nov 04, 2011 4:38 pm
Re: Variable outside of scope
definitely!Radikalizm wrote:Maybe you should review your C++ skills before you go on?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!