Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compile time constants - few questions...

Reply
Thread Tools

Compile time constants - few questions...

 
 
SpOiLeR
Guest
Posts: n/a
 
      03-13-2005
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-13-2005
"SpOiLeR" <request@no_spam.org> wrote...
> 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


 
Reply With Quote
 
 
 
 
oxyd.oxyd@gmail.com
Guest
Posts: n/a
 
      03-13-2005
SpOiLeR wrote:
> 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

 
Reply With Quote
 
SpOiLeR
Guest
Posts: n/a
 
      03-13-2005
On 13 Mar 2005 10:08:46 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> SpOiLeR wrote:
>> ...

>
> .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...
 
Reply With Quote
 
SpOiLeR
Guest
Posts: n/a
 
      03-13-2005
On Sun, 13 Mar 2005 12:57:53 -0500, Victor Bazarov wrote:

> "SpOiLeR" <request@no_spam.org> wrote...
>> ...

>
> 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...
 
Reply With Quote
 
qfel
Guest
Posts: n/a
 
      03-15-2005
> 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?


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-15-2005
qfel wrote:
>>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?


'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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
computation at compile time i.e. compile time functions usingtemplates Carter C++ 2 03-04-2009 06:43 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
Per-type compile time constants? mario@spamnotme.it C++ 3 08-13-2006 12:33 PM
Brainstorm for compile-time constants Frederick Gotham C++ 2 06-25-2006 09:46 PM
Compile time constants in classes michael C++ 6 03-03-2006 06:54 AM



Advertisments