Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

spiffgq

How do I define a constant expression with in a class?

This topic is 5813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My program was written using GCC and now I''m trying to port it to MSVC++ 6.0 (which is proving to be a big headache). I''ve managed to fix most of the errors, but there is one big problem that VC++ bitches about that GCC doesn''t care about:
  
class MyClass {

   static const int MY_CONST = 1234;

};
  
I get the errors :
error C2258: illegal pure syntax, must be ''= 0''
error C2252: ''MY_CONST'' : pure specifier can only be specified for functions
 
This can be fixed by putting the constant in the source file instead of the header file. But there is one case where I can''t move it:
  
class OtherClass{

   static const int MY_CONST; // this is assigned a value in the source file instead of here

 
   int arr[OtherClass::MY_CONST];

};
  
Now, if I try to compile it,
error C2057: expected constant expression
warning C4200: nonstandard extension used : zero-sized array in struct/union
 
Is there some way to fix this knot? Thanks for your help.

Share this post


Link to post
Share on other sites
Advertisement
First, static and const are redundant, and I believe static can only be used for functions, though I might be wrong. I would recommend whacking the static part of your code and trying again. That should fix the problem. Otherwise, use an enumeration (enum).

Then again, I''m one of the most hated people in all of computer programming...a Borland user. Ahh! Run for your lives!

Share this post


Link to post
Share on other sites
Whats''s wrong with Borland?

At least Borland lets you do:

for (int i=sometging; blah; blah)
for (int i=sometging; blah; blah)
for (int i=sometging; blah; blah)

But MS complains about the earlie declaration of i. Bah.

Share this post


Link to post
Share on other sites
quote:
Original post by Racso
First, static and const are redundant, and I believe static can only be used for functions, though I might be wrong.


Actually, you''re wrong on both accounts. Static and const are two completely different things. He wants a single constant used by all instances of the class. If he did just static, then the value would be editable, if he did just const, then there would be an instance of it in every single object! He doesn''t want either of those he wants them both at the same time. Thinking static is only applicable to functions makes me think that you probably don''t understand what static means.

Anyways, the enum version is the best -- while it doesn''t make a constant in memory, it gives the same effect as what you want (unless at some point you want to use the value''s memory address because with an enum it won''t have one). Don''t put extra stuff in memory when you can avoid it. Enumerations are fine in this case.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
In a .h file

class foo
{
public:
static const int CONST;
}

in a .cpp

const int foo::CONST = 1234;

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
In a .h file

class foo
{
public:
static const int CONST;
}

in a .cpp

const int foo::CONST = 1234;


He knows he can do that, but his point is that he has another variable in the class that is dependent on the value. In this case, what you mentioned will not solve his problem. Using enum is what he should be doing.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!