Thread: Defining const in a class

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Defining const in a class

    Sorry if the question is rather elementary. You see, I've never defined constants with "const" keyword. I've always defined it in a macro. But apparently KDevelop can't recognize macro constant in the "CTRL+Space" hotkey (which is rather annoying). And defining the constants in global variables is a bad practice. So, the question is simple really, how do I define constants with "const typedef" keyword in a class? I've tried defining it in the ".h" and failed e.g.:
    Code:
    class a
    {
    private:
      const int c_iTheConst = 10000; //Failed!!!
    };
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I think you have to initialize it with a member initializer like:

    Code:
    class a : c_iTheConst(10000)
    {
    private:
      const int c_iTheConst; 
    };
    Edit: Ignore that, you use the member initializer in the constructor instead. My bad.
    Last edited by robatino; 11-19-2006 at 10:52 PM.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Translation:

    Code:
    class a
    {
    public:
            a() : c_iTheConst(10000) { }
    private:
            const int c_iTheConst;
    };
    Note that I used a() : c_iTheConst(10000) { } and not a() { c_iTheConst = 100000; }

  4. #4
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Thanks. But what if the constant was a struct?

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    You should give the struct a constructor that initialises its members in an initialisation list, then use something like this:
    Code:
    struct s
    {
        s( const int m1_, const int m2_ ) : m1(m1_) , m2(m2_)
        {}
        const int m1;
        const int m2;
    };
    
    class a
    {
    public:
            a() : c_iTheConst(10000) , s(3030, 10349) { }
    private:
            const struct s const_struct;
            const int c_iTheConst;
    };

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If the const has the same value for all instances of the class, then you should make it static as well. If it is a static const int, you should be able to initialize it in the class body (although some older compilers might not allow it). For the struct, I don't remember if you can initialize it in the class body or not, but either way if all instances are using the same const value it should be static.

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. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Help with CString class and operator+ overloading
    By skanxalot in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 10:23 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM