Brutile Posted May 11, 2014 Share Posted May 11, 2014 If anyone knows C++, please help... I'm trying to initialize a constant variable using a static variable from another class. I want to assign: // a.h static const short worldHeight = 128; To: // b.h static const short height = a::worldHeight; So I can then do this: // b.h bool stuff[height]; But I get errors. Anyone know how to achieve this? Quote Link to comment Share on other sites More sharing options...
Rick Posted May 11, 2014 Share Posted May 11, 2014 I don't use constants all that often but as far as I'm aware constants are basically literals (meaning you have to assign literals to them and not variables). Why can't you just pass a::worldHeight to the stuff array? If you really want the height variable don't make it constant. Also, if you ever get errors it's best to show us the errors in your post. It'll help us not have to guess. Quote Link to comment Share on other sites More sharing options...
Brutile Posted May 11, 2014 Author Share Posted May 11, 2014 I can't pass it directly into the stuff array either. The errors are: 1. use of undefined type 'a' 2. 'worldHeight': undeclared identifier 3. expected constant expression Even though everything is declared at compile time. Alternatively, If anyone knows how to make a class that holds variables that only need to be assigned once, and can be used for all classes. I'm only doing this so I don't need to make multiple instances of the same value. Quote Link to comment Share on other sites More sharing options...
Brutile Posted May 11, 2014 Author Share Posted May 11, 2014 I kinda found a solution. I just declared the array as bool*** stuff; then did stuff*** = new bool**[num]; then looped and did stuff**[x] = new bool*[num]; then another loop and did stuff*[x][y] = new bool[num]; etc. It isn't as quick as declaring the array at compile time, but it works, so I'm happy Quote Link to comment Share on other sites More sharing options...
catch22 Posted May 11, 2014 Share Posted May 11, 2014 So, this is what defines are for generally. If your worldheight is going to be 128 no matter what, #define it in your particular header. #define WORLDHEIGHT 128 (that's case sensitive btw. Most programmers caps their defines or use a way to recognize them. I prefer the old school 'k' prefix such as kWorldHeight -- all caps are fugly to my old eyes, but it's your style not mine!) 1 Quote Coding for Christ. Link to comment Share on other sites More sharing options...
catch22 Posted May 11, 2014 Share Posted May 11, 2014 Alternatively, If anyone knows how to make a class that holds variables that only need to be assigned once, and can be used for all classes. I'm only doing this so I don't need to make multiple instances of the same value. This is a very pythonic concept, actually. Since you can't really do globals in python (hallelujah), you can mimic the behavior by making a class that holds your predefined values, and then just import that class across your program files to access the "globals" which are just properties of a local class instance (at that point). I still think #define is what you're after though. You'll want to make a header called like "globals.h" and use "#pragma once" at the top of the file, to avoid header fighting throughout your program. While non standard, just about everything respects it, and it's much simpler than old school header guards. Quote Coding for Christ. Link to comment Share on other sites More sharing options...
Josh Posted May 11, 2014 Share Posted May 11, 2014 The simple answer is don't ever do this, ever. Here is the long answer: http://www.parashift.com/c++-faq/static-init-order.html Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Brutile Posted May 11, 2014 Author Share Posted May 11, 2014 catch22, you are a life saver! that #define works just how I wanted it to. Thank you and everyone else that helped. I can now clean up my code a bit. Quote Link to comment Share on other sites More sharing options...
Brutile Posted May 11, 2014 Author Share Posted May 11, 2014 Here's another question... can I access a #define from another class? Can it be accessed if I include the header or something? Quote Link to comment Share on other sites More sharing options...
Rick Posted May 11, 2014 Share Posted May 11, 2014 That's what catch22 is talking about in his last post last paragraph. // globals.h #pragma once #define MAP_HEIGHT = 128 Now in any file you need this just: #include "globals.h" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.