-2

Folks,

Problem Statement - Does C++ allow a (static) const be limited to a class scope, so I can get rid of #defines that pollute entire namespace?

My observation is NO (in the following DIFFERENT examples), and I'd like to find out why and what's the best alternative. VS generates error C2589: 'const' : illegal token on right side of '::'

EXAMPLE1

// a.h

class A {

…
.. 
static const uint_32 myConst = 1234;

};

//b.cpp

include “a.h”

… B() { uint32_t arr[A::myConst]; // C2589! const : illegal token on right side of '::' }

EXAMPLE 2

// a.h

class A { … .. enum blah { ... myConst = 1234, .. }; };

//b.cpp

include “a.h”

... B() { uint32_t arr[A::myConst]; // C2589! const : illegal token on right side of '::' }

  • @DGomez thats not the only thing, he essentially wrote `a::1234` – aaronman Nov 08 '13 at 18:48
  • 1
    The above might look like code superficially, but it is not. If you are having problems getting code to compile, post a http://sscce.org and people can help. To fix your problem above, people would first have to read your mind, and this website is not a psychic help line. – Yakk - Adam Nevraumont Nov 08 '13 at 18:51
  • Another potential problem with the failing cases is that visibility of class members is by default private, so unless you've got those constants/enums defined after a `public:` tag, you can't use them in a different, unrelated class. However, you didn't post enough of your code to know if that is actually the case... – twalberg Nov 08 '13 at 19:37

3 Answers3

12

When you take your macro:

#define CONST 1234

and substitute it for where you use it:

static const int CONST = 1234;

The end result is nonsense:

static const int 1234 = 1234;

In another instance:

   Int a1[a::CONST];  

This also becomes nonsense:

   Int a1[a::1234];  

This all begs the question, what are you trying to do?


It looks like you're trying to create a member variable with the same name as your macro, CONST, here:

class A
{
  static const int CONST = 1234;
};

However since when this code is compiled the macro has already been defined, the preprocessor changes this by substituting the macro before the compiler itself can get a crack at it. By the time the code is compiled, it looks like this:

class A
{
  static const int 1234 = 1234;
};

Best is to just do away with the macro entirely, and then retrofit your code to use proper constants like you're trying to do here. Don't mix and match. At the very least, don't use the same name for the member as you do for the macro.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
7

First of all your class is called A, as in capital A, not a. The class name is used to qualify the constant you are trying to use. So, change your code to use A::CONST. By the way, this is C++ not C# or Java, so there is no such thing as an Int, unless for some bizarre reason you decided to invent your own integer type.

As an aside, using all caps to name constants can collide with macros and is a good way to get into trouble, especially since pre-processing happens first and macros are substituted for all cases of the constants. This can often lead to invalid C++ code with syntax errors that are difficult to understand. That's why you should never use all caps to name constants, since this is a convention most commonly used for macros.

Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
  • 2
    I'm always amazde that a useless answer becomes upvoeted if the poster has enough reputation, while the correct answer like this one, is not upvoted. +1. – Devolus Nov 08 '13 at 18:52
  • @Devolus: Perhaps you could help by suggesting how my answer could be improved, instead of passive-aggressively complaining about how it's "useless?" – John Dibling Nov 08 '13 at 18:58
  • @JohnDibling, The poster posted two approaches, one with a macro and one with a `const`, and made a mistake. So the correct answer is not how macros work, rather it is simply that the compiler makes a distinction between upper- and lowercase. While your answer is technically correct, it doesn't address the the problem, as it was not about a problem with macros. But I didn't critizice you, my comment was aimed at these voters who don't really read what a question is about and simply follow the herd, which I notice rather often here. If the rep is high, voters tend to upvote even wrong answers. – Devolus Nov 08 '13 at 22:30
0

If I may make a guess, it looks like you're trying to use :: the same way you use . in Python.

It looks like you really don't understand what the scope resolution operator does, or how it works.

:: has a very specific, and quite limited usage. Until you understand it better, we're going to have a really hard time helping you.

Matt
  • 775
  • 7
  • 24