Connecting Tech Pros Worldwide Help | Site Map

Compile time constants - few questions...

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 01:59 AM
SpOiLeR
Guest
 
Posts: n/a
Default Compile time constants - few questions...

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.

  #2  
Old July 23rd, 2005, 01:59 AM
Victor Bazarov
Guest
 
Posts: n/a
Default 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


  #3  
Old July 23rd, 2005, 01:59 AM
oxyd.oxyd@gmail.com
Guest
 
Posts: n/a
Default 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

  #4  
Old July 23rd, 2005, 01:59 AM
SpOiLeR
Guest
 
Posts: n/a
Default 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...
  #5  
Old July 23rd, 2005, 01:59 AM
SpOiLeR
Guest
 
Posts: n/a
Default 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...
  #6  
Old July 23rd, 2005, 02:04 AM
qfel
Guest
 
Posts: n/a
Default 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?


  #7  
Old July 23rd, 2005, 02:04 AM
Victor Bazarov
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.