Jump to content

C++ initializing const variable using static variable from another class


Brutile
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!)

  • Upvote 1

Coding for Christ.

Link to comment
Share on other sites

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.

Coding for Christ.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...