C++ Help please (easy)

Associate
Joined
24 Sep 2003
Posts
928
Location
Bedfordshire
Hi,

Got a number of classes that need to access some integer variables (determined before runtime in main) that won't change. Is it best to setup a super class to hold these to access them from the other classes?

Thanks,

aaazza
 
In C++, the best way to define these is as constants, ideally inside a namespace. Don't force them into a base class. The term 'superclass' by the way is not a C++ one, use base and derived instead.

So, you do this:

in some header file:

Code:
namespace valued_monkeys
{
  const int my_value_one = 1;
  const int my_value_two = 2;
}
 
Ah ok cool.

If I do this in a header and include it in other classes I can just access them?

EDIT: Of course you just use 'using namespace namespace_name;' :)

Cheers,

aaazza
 
Last edited:
If I do this in a header and include it in other classes I can just access them?

EDIT: Of course you just use 'using namespace namespace_name;'

No, you do 'valued_monkeys::my_value_one'. Litering your code with 'using namespace' directives defeats the point of using namespaces in the firstplace.
 
Back
Top Bottom