Compile time constants - few questions...

S

SpOiLeR

Hi!

q1:
What is C++ equivalent for:

#define MY_CHAR_ARRAY_CONSTANT "My constant"

if I want to define that constant in protected area of some class?
I assume it has something to do with static const data members but can't
figure out right syntax.

q2:
If I have something like this:

static const int MY_CONST = 5;

in some class, is it really equivalent to this:

#define MY_CONST 5

By "equivalent" I mean that it is real compile time constant where MY_CONST
is replaced my preprocessor and it is not taking up any memory during
runtime.
 
V

Victor Bazarov

SpOiLeR said:
q1:
What is C++ equivalent for:

#define MY_CHAR_ARRAY_CONSTANT "My constant"

if I want to define that constant in protected area of some class?
I assume it has something to do with static const data members but can't
figure out right syntax.

Data members if non-static, have only one syntax and that syntax
cannot contain initialisation. Any and all initialisation is done
in the object's constructor.

If the member is static (which seems more appropriate as a substitute
for a preprocessor macro), initialisation is done with the definition
which has to exist at the namespace level.

class A {
protected:
static const char MY_CHAR_ARRAY_CONSTANT[];
};

const char A::MY_CHAR_ARRAY_CONSTANT[] = "My constant";
q2:
If I have something like this:

static const int MY_CONST = 5;

in some class, is it really equivalent to this:

#define MY_CONST 5

By "equivalent" I mean that it is real compile time constant where
MY_CONST
is replaced my preprocessor and it is not taking up any memory during
runtime.

Depends on its use. Most likely, yes, it is equivalent. Of course,
that constant is not visible outside the class definition compared
to the macro which has global scope.

V
 
O

oxyd.oxyd

SpOiLeR said:
Hi!

q1:
What is C++ equivalent for:

#define MY_CHAR_ARRAY_CONSTANT "My constant"

const std::string MY_STRING_CONSTANT = "My constant";
or
const char *const MY_CHAR_ARRAY_CONSTANT = "My constant";
if I want to define that constant in protected area of some class?
I assume it has something to do with static const data members but can't
figure out right syntax.

Um... For example, like this:

..h file:
class C {
protected:
static const int CONSTANT;
};

..cpp file:
const int C::CONSTANT = 4;
q2:
If I have something like this:

static const int MY_CONST = 5;

in some class, is it really equivalent to this:

#define MY_CONST 5

By "equivalent" I mean that it is real compile time constant where MY_CONST
is replaced my preprocessor and it is not taking up any memory during
runtime.

Yes. The compiler will replace it during compilation. However, when you
do something weird to the constnat, like getting its address (const int
*p = &MY_CONST), the compiler will generate regular variable (for it
has to have som e address).

But in general, the answer is yes.

Oxyd
 
S

SpOiLeR

.cpp file:
const int C::CONSTANT = 4;

That's what I've been missing...
Yes. The compiler will replace it during compilation. However, when you
do something weird to the constnat, like getting its address (const int
*p = &MY_CONST), the compiler will generate regular variable (for it
has to have som e address).

But in general, the answer is yes.

Oxyd

OK, now I understand much better. Thanks...
 
S

SpOiLeR

SpOiLeR said:

class A {
protected:
static const char MY_CHAR_ARRAY_CONSTANT[];
};

const char A::MY_CHAR_ARRAY_CONSTANT[] = "My constant";

Ah, this sheds a bright light on syntax part of topic :)
Of course,
that constant is not visible outside the class definition compared
to the macro which has global scope.

V

Which is exactly why I want it inside the class... Thanks...
 
Q

qfel

initialisation is done with the definition
which has to exist at the namespace level.
Isn't it required only for arrays, classes etc.?
So
class A
{
static const /*doesn't const imply static?*/ int max_A=12313;
};
Would be legal?
 
V

Victor Bazarov

qfel said:
Isn't it required only for arrays, classes etc.?
So
class A
{
static const /*doesn't const imply static?*/ int max_A=12313;
};
Would be legal?

'const' does NOT imply static. And, yes, initialising this way is allowed
if 'max_A' has an integral type. And you don't need to provide the proper
definition if it is NOT used outside the class A definition. If it is
used outside, then you have to _define_ it outside as well, but you must
omit the initialiser. Yes, I know, a bit involved...

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top