Thread: Using Const - Embarressed to Ask

  1. #1
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812

    Using Const - Embarressed to Ask

    Hi there,

    Here's a noddy question. How do I use const to define constants instead of #define?

    Everything I read tells me to use const instead of #define in C++, but I've yet to see an example. So...

    How how do I declare a const int in a header file?

    Can I declare (and define) a const inside a class header?

    Cheers
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I believe it's the same as a regular variable only you precede the whole statement with the const keyword.

    e.g. const int pop = 213;

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Can't do this? No?

    Code:
    class Test
    {
    public:
      const int A = 5;
    };
    RESULT:
    [C++ Error] E2233 Cannot initialize a class member here
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Try this instead:
    Code:
    class Test {
        pubilc:
            enum { A = 5 };
    };
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Const members (nonstatic) can ONLY be initialized in the initialization list
    Code:
    class Test
    {
    public:
    Test();
    private:
    const int A;
    };
    
    Test::Test():
    A(5)
    {}
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >Try this instead...

    That's a nice idea. Thanks.

    I've found I can do this in with BCB:

    Code:
    class Test {
        pubilc:
            static const int A = 5;
    };
    But it won't compile with VC6.

    Is this a standards compliance issue?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Davros
    Can't do this? No?

    Code:
    class Test
    {
    public:
      const int A = 5;
    };
    RESULT:
    [C++ Error] E2233 Cannot initialize a class member here

    That won't work because when you just say
    const int A
    int class definition, that means you want A to be a const datamember of every instance of the class!

    Instead, you have to make it a static const

    Code:
    class Test
    {
    public:
      static const int A = 5;
    };
    On a compliant compiler that should work, however, if you are using an old compiler (IE visual C++ 6.0) that will probably give you an error. So if your compiler is choking on that, you can use an enum instead

    Code:
    class Test
    {
    public:
      enum { A = 5 };
    };
    The only problem with that is it won't be of type int, but an unnamed integral enum type that can be implicitly converted to an int.

    Edit: o man, late

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Thanks.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM