Thread: Constants in the scope of classes

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    43

    Constants in the scope of classes

    Let us have this code:

    struct TestOne
    {
    enum
    {
    MY_CONSTANT = 12356
    }

    };

    That was OK, as MY_CONSTANT has not memmory for it, it is a compile time constant inside of scope of TestOne (TestOne::MY_CONSTANT)

    Let now we want constant which is double. "enum" doesn't want doubles, so what should we do?
    How can we create a constant, with no memmory for it inside of the scope of class?

    E.g. the code:
    struct TestOne
    {
    static const double MY_CONSTANT = 12356.2 ;
    };

    is not compiled with Visual C++60 SP4, neither with Borland C++ 5.02

    Any ideas?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Make it static and private and stop worrying about 8 bytes.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    as mentioned above you should make it a static member.
    However the code you wrote won't compile. static memebers are declared inside the class, but should be defined outside of hte class definition.
    In the h file, you write:

    struct TestOne
    {
    static const double MY_CONSTANT;
    };


    and in the cpp file, you write
    const double TestOne::MY_CONSTANT =12356.2 ;

    Good luck

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    43

    Wink


    Yes, but it is a question of principles
    I do not need theese 8 bytes!
    There is solution but I managed to compile it only in GNU C++:

    static const double blabla = 0.2;

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    43

    But static is slower

    According to the book "Thinking in C++" if you use static for const declarations, you can initialize them inside a class. And the code is compiled by GNU C++.
    But not by MSVC++ & Borland C++. If I make it static, that would make my program a little bit slower, as redirection will be needed to find the exact constant e.g.

    double d = TestStruct::MY_CONSTANT; //Will extract data
    //then copy to "d"
    double d = 0.23; //Will directly fill "d"
    //with supplied by
    //compiler data

    Maybe inline function will do the work
    e.g.
    struct TestStruct
    {
    static double MY_CONSTANT() {return 0.23; }
    }

    //but the code is ugly - you should use braces

    double d = TestStruct::MY_CONSTANT();

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    If you define the static outside the class it should have no impact on performance. It is surely better then a function (even inlined).
    Static objects are intialized once durint program startup.

    When assigned to a variable:

    double d = TestStruct::MY_CONSTANT;

    it should be the same as

    double d = 0.23;

    it is the same as

    void func() {
    double a = 0.1;
    double d = a;
    }

    the assinment to a has no redirection - it's a simple memory copy.
    A good compiler should optimize your code, if you never take the addres of your static double objects, it can treat them as actual numbers when you reference them in your code. So it should be as good as assignment.
    In general, before making assumptions regarding what has better performance, you should profile your program and see what takes time, and whether other alternatives improves it. Just guesing ignor many details of compiler internal roblems and optimizations.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    Ah, yes profiling is good, I am absolutely sure, that this is not the point which slows my program.
    But understanding the way Intel processors work I know, that copying from memory to memory is slower, than loading of constant to register, or memory. See a little assembly code your program generates...
    Also the static variable could be in another module (e.g. dll), which for sure will have a redirection to find it.
    These were simple problems, now look at the serious ones:

    in source1.cpp
    //static initialization
    double TestStruct::MY_CONSTANT = 2.0;

    in source2.spp
    //again initialization of static variable:
    double Something = TestStruct::MY_CONSTANT;

    In C++ you have no guarantee in what order the two variables will be initialized, so you may find out that "Something" has a 0.0 value. This is a real problem, which I already had in real situations with a large project

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    yeah, me too

    well to solve that - if you're not sure if your static memeber will be used in static objects, I'd use an inline method as you suggested.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM