Compile time constants - few questions...

Discussion in 'C++' started by SpOiLeR, Mar 13, 2005.

  1. SpOiLeR

    SpOiLeR Guest

    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.
     
    SpOiLeR, Mar 13, 2005
    #1
    1. Advertising

  2. "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
     
    Victor Bazarov, Mar 13, 2005
    #2
    1. Advertising

  3. SpOiLeR

    Guest

    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
     
    , Mar 13, 2005
    #3
  4. SpOiLeR

    SpOiLeR Guest

    On 13 Mar 2005 10:08:46 -0800, 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...
     
    SpOiLeR, Mar 13, 2005
    #4
  5. SpOiLeR

    SpOiLeR Guest

    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...
     
    SpOiLeR, Mar 13, 2005
    #5
  6. SpOiLeR

    qfel Guest

    > 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?
     
    qfel, Mar 15, 2005
    #6
  7. 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
     
    Victor Bazarov, Mar 15, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    580
  2. michael
    Replies:
    6
    Views:
    331
    Victor Bazarov
    Mar 3, 2006
  3. Murali
    Replies:
    2
    Views:
    611
    Jerry Coffin
    Mar 9, 2006
  4. Frederick Gotham

    Brainstorm for compile-time constants

    Frederick Gotham, Jun 25, 2006, in forum: C++
    Replies:
    2
    Views:
    342
    Rolf Magnus
    Jun 25, 2006
  5. Carter
    Replies:
    2
    Views:
    528
    Carter
    Mar 4, 2009
Loading...

Share This Page