Connecting Tech Pros Worldwide Forums | Help | Site Map

Compile time constants - few questions...

SpOiLeR
Guest
 
Posts: n/a
#1: Jul 23 '05
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.

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Compile time constants - few questions...


"SpOiLeR" <request@no_spam.org> wrote...[color=blue]
> 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.[/color]

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";
[color=blue]
>
> 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.[/color]

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


oxyd.oxyd@gmail.com
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Compile time constants - few questions...


SpOiLeR wrote:[color=blue]
> Hi!
>
> q1:
> What is C++ equivalent for:
>
> #define MY_CHAR_ARRAY_CONSTANT "My constant"
>[/color]

const std::string MY_STRING_CONSTANT = "My constant";
or
const char *const MY_CHAR_ARRAY_CONSTANT = "My constant";
[color=blue]
> 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[/color]
can't[color=blue]
> figure out right syntax.
>[/color]

Um... For example, like this:

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

..cpp file:
const int C::CONSTANT = 4;
[color=blue]
> 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[/color]
MY_CONST[color=blue]
> is replaced my preprocessor and it is not taking up any memory during
> runtime.[/color]

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

SpOiLeR
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Compile time constants - few questions...


On 13 Mar 2005 10:08:46 -0800, oxyd.oxyd@gmail.com wrote:
[color=blue]
> SpOiLeR wrote:[color=green]
>> ...[/color]
>
> .cpp file:
> const int C::CONSTANT = 4;
>[/color]

That's what I've been missing...
[color=blue]
>
> 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[/color]

OK, now I understand much better. Thanks...
SpOiLeR
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Compile time constants - few questions...


On Sun, 13 Mar 2005 12:57:53 -0500, Victor Bazarov wrote:
[color=blue]
> "SpOiLeR" <request@no_spam.org> wrote...[color=green]
>> ...[/color]
>
> class A {
> protected:
> static const char MY_CHAR_ARRAY_CONSTANT[];
> };
>
> const char A::MY_CHAR_ARRAY_CONSTANT[] = "My constant";[/color]

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

Which is exactly why I want it inside the class... Thanks...
qfel
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Compile time constants - few questions...


> initialisation is done with the definition[color=blue]
> which has to exist at the namespace level.[/color]
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?


Victor Bazarov
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Compile time constants - few questions...


qfel wrote:[color=blue][color=green]
>>initialisation is done with the definition
>>which has to exist at the namespace level.[/color]
>
> 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?[/color]

'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
Closed Thread