How to define class final constants?

Discussion in 'C++' started by Allen, Nov 28, 2006.

  1. Allen

    Allen Guest

    I defines FileLogConstant class as following.

    class FileLogConstant
    {
    public:
    static const INT32 REGISTER_LOGGER;

    static const INT32 REGISTER_METHOD;

    static const INT32 MAX_LOGGER_COUNT;

    static const INT32 MAX_LOGGER_METHOD_COUNT;

    static const INT32 MAX_LOG_MESG_COUNT;

    static const INT32 MAX_REG_COUNT;

    static const INT32 MAX_FILE_LENGTH;
    };

    /////////////////////////////////////////////BLOCK
    DEF//////////////////////////////////////////////////////////////

    const INT32 FileLogConstant::REGISTER_LOGGER = 0x00;

    const INT32 FileLogConstant::REGISTER_METHOD = 0x01;

    const INT32 FileLogConstant::MAX_LOGGER_COUNT = 64;

    const INT32 FileLogConstant::MAX_LOGGER_METHOD_COUNT = 128;

    const INT32 FileLogConstant::MAX_LOG_MESG_COUNT = 128;

    const INT32 FileLogConstant::MAX_REG_COUNT = 16;

    const INT32 FileLogConstant::MAX_FILE_LENGTH = 4 + 4 +
    FileLogConstant::MAX_LOGGER_COUNT *
    (16 + 4 + 64 * FileLogConstant::MAX_LOGGER_METHOD_COUNT) + 4 + 4 +
    FileLogConstant::MAX_LOG_MESG_COUNT * (2 + 2 + 4 + 4 + 256) +
    FileLogConstant::MAX_REG_COUNT * (2 + 2 + 64);

    /////////////////////////////////////////////////BLOCK
    DEF////////////////////////////////////////////////////////////

    class Foo
    {
    public:
    char buffer[FileLogConstant::MAX_FILE_LENGTH];
    };

    when BLOCK DEF is put in FileLogConstant.cpp file, the buffer
    declaration of class Foo will be compiled with an error. While BLOCK
    DEF is put in FileLogConstant.h file, the compiler will generate many
    const FileLogConstant::MAX_FILE_LENGTH already defined errors.

    How to define class final constants?
     
    Allen, Nov 28, 2006
    #1
    1. Advertisements

  2. * Allen:
    Don't use all uppercase except for macros (which should all be all
    uppercase).

    Apart from the all uppercase name (don't) this is technically OK. But I
    suspect that you have placed this definition in a file that you include
    more than once. If it's included more than once in the same translation
    unit then the compiler will complain, and otherwise, if it's included in
    more than one translation unit the linker will complain, because you're
    then violating the One Definition Rule.
     
    Alf P. Steinbach, Nov 28, 2006
    #2
    1. Advertisements

  3. Allen

    Allen Guest

    Thank you.

    I want to replace macro constant definitions with class member
    constants. But
    can these member constants directly used in the array dimension of
    class member
    declaration? i.e.

    class Foo
    {
    public:
    char buffer[ConstantCls::constant1];
    };
     
    Allen, Nov 28, 2006
    #3
  4. Allen

    David Harmon Guest

    On 27 Nov 2006 18:00:23 -0800 in comp.lang.c++, "Allen"
    Yes.
     
    David Harmon, Nov 28, 2006
    #4
  5. Allen

    Allen Guest

    "David Harmon дµÀ£º
    Do you have sample codes?
    I tried but failed.
    The compiler tells two kinds of errors, redefinition and non-static
    dimension.
     
    Allen, Nov 29, 2006
    #5
  6. Allen

    BobR Guest

    Allen wrote in message ...

    /* """
    "David Harmon дµÀ£º
    Do you have sample codes?
    I tried but failed.
    The compiler tells two kinds of errors, redefinition and non-static
    dimension.

    """ */

    class Allen{
    static const size_t Max = 5;
    public:
    Allen();
    };
     
    BobR, Nov 29, 2006
    #6
  7. Allen

    Allen Guest

    This code cannot be compiled.

    What I want is class member constant used directly by array dimension
    declaration:

    class Allen{
    static const size_t Max;
    static char buffer[Max]; // Max cannot be used here. I do want.
    public:
    Allen();
    };
     
    Allen, Nov 29, 2006
    #7
  8. Allen

    David Harmon Guest

    On 29 Nov 2006 00:16:36 -0800 in comp.lang.c++, "Allen"
    static const size_t Max = 73;
     
    David Harmon, Nov 29, 2006
    #8
  9. Allen

    kwikius Guest

    ? It should compile OK AFAICS :

    #include <iostream>

    // static integral constants can be initialised inline:
    class Allen{
    static const size_t Max = 5; //fine
    public:
    Allen(){}
    };

    class Allen1{
    static const size_t Max = 5; //fine

    public:
    static char buffer[Max]; // declaration fine
    Allen1(){}
    };

    char Allen1::buffer[] = "fine"; // member definition in cpp file

    int main()
    {
    std::cout << Allen1::buffer <<'\n';
    }

    regards
    Andy Little
     
    kwikius, Nov 29, 2006
    #9
  10. Allen

    Allen Guest

    No. It cannot be compiled by VC++ 6.0.
    I will test with gcc.

    "kwikius дµÀ£º
    "
     
    Allen, Nov 30, 2006
    #10
  11. Allen

    kwikius Guest

    VC6 has a lot of problems with implementing the C++ standard correctly,
    IOW its probably the compiler in this case that is letting you down.
    If it fails with gcc then again try a more recent version if possible,
    but if it does fail then you probably have a quite old version of gcc.

    regards
    Andy Little
     
    kwikius, Nov 30, 2006
    #11
  12. Allen

    Allen Guest

    "kwikius дµÀ£º
    "
    When was the newest C++ standard put forward?
    VC6 may be implement C++ standard before 1998.
     
    Allen, Nov 30, 2006
    #12
  13. Allen

    kwikius Guest

    According to my copy of the C++ standard, BS ISO/IEC 14882 (the C++
    standard) was ratified in 1997, however AFAIK Microsoft Borland and
    others saw the potential of C++ for Windows development way before that
    of course and so produced compilers predicting I guess what the
    standard would be and also perhaps more importantly trying to keep
    compatibility with their previous software. Also the C++ templates
    specification was rushed into the standard quite late as far as I know,
    which of course put existing compilers in a tricky position, IOW you
    can't blame them for that and MSVC1.0 was my first C++ compiler which I
    am really grateful to for introducing me to the language.

    As I understand it the template specs were put in the standard without
    anyone really having a working compiler that implemented them
    correctly. It seems that Bjarne Stroustrup must have cast some sort of
    spell over the Approval committee, which even he was surprised actually
    worked if I remember from reading his book '"The design and evolution
    of C++". So this is the sort of problem compiler writers faced at that
    time.

    Also I think that the ability to define static integral constants
    inside the class definition was another late addition.

    For the MS series both VC7.1 (not VC7.0) and VC8.0 do a good job of
    implementing the current Standard.

    As for gcc I curreently have gcc 4.0 , for earlier versions I am not so
    sure, but a wild guess is to try to get a version after around gcc 3.3
    or so.

    And even in the latest compilers, if you look hard enough, you can find
    something that is not correct.

    That is part of the mystique of C++ FAICS. No one will ever truly
    master it. And of course as soon as compilers get up to speed there
    will be a new version of the standard and the whole rigmarole of non
    conforming compilers will continue as usual :)

    regards
    Andy Little
     
    kwikius, Nov 30, 2006
    #13
  14. Allen

    BobR Guest

    Allen wrote in message ...
    /* """
    "kwikius дµÀ£º
    "
    When was the newest C++ standard put forward?
    VC6 may be implement C++ standard before 1998.

    """ */

    Ah ha, old compiler. Then maybe you can use the old hack:

    class Allen{
    enum { size = 1000 };
    int i[size];
    // .....
    };
     
    BobR, Nov 30, 2006
    #14
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.