How to define a constant integer inside a class with keyword const ?

Discussion in 'C++' started by b83503104, May 24, 2004.

  1. b83503104

    b83503104 Guest

    I know this is illegal:
    class XYZ {
    const int myConst = 1;
    ....};

    But then, does it mean I have to use #define ?
    Thanks
    b83503104, May 24, 2004
    #1
    1. Advertising

  2. b83503104

    Jeff Schwab Guest

    Re: How to define a constant integer inside a class with keywordconst ?

    b83503104 wrote:
    > I know this is illegal:
    > class XYZ {
    > const int myConst = 1;
    > ...};
    >
    > But then, does it mean I have to use #define ?


    No, you don't have to use #define to define a constant in C++.
    Jeff Schwab, May 24, 2004
    #2
    1. Advertising

  3. "b83503104" <> wrote in message
    news:...
    > I know this is illegal:
    > class XYZ {
    > const int myConst = 1;
    > ...};
    >
    > But then, does it mean I have to use #define ?
    > Thanks


    class XYZ {
    static const int myConst = 1;
    ...};

    Now its legal. You could also consider

    class XYZ {
    enum { myConst = 1 };
    ...};

    Don't use a #define what ever you do. Apart from anything else #defines are
    never inside a class.

    john
    John Harrison, May 24, 2004
    #3
  4. b83503104

    JKop Guest

    Jeff Schwab posted:

    > b83503104 wrote:
    >> I know this is illegal:
    >> class XYZ {
    >> const int myConst = 1;
    >> ...};
    >>
    >> But then, does it mean I have to use #define ?

    >
    > No, you don't have to use #define to define a constant in C++.
    >



    Not very helpful. Quite ignorant actually. Asshole.


    Here's how it's done:


    class XYZ
    {
    public:

    const unsigned int chocolate;

    XYZ(void) : chocolate(53)
    {

    }

    };


    If it's a static variable, it's done as so:


    class XYZ
    {
    public

    static const unsigned int chocolate;

    XYZ(void)
    {

    }
    };


    const unsigned int XYZ::chocolate = 53;



    Hope that helps.



    -JKop
    JKop, May 24, 2004
    #4
  5. b83503104

    JKop Guest

    John Harrison posted:

    >
    > "b83503104" <> wrote in message
    > news:...
    >> I know this is illegal:
    >> class XYZ {
    >> const int myConst = 1; ...};
    >>
    >> But then, does it mean I have to use #define ?
    >> Thanks

    >
    > class XYZ {
    > static const int myConst = 1;
    > ...};
    >
    > Now its legal. You could also consider
    >
    > class XYZ {
    > enum { myConst = 1 };
    > ...};
    >
    > Don't use a #define what ever you do. Apart from anything else #defines
    > are never inside a class.
    >
    > john



    Incorrect.


    Even with a static variable you must do the following:


    class XYZ
    {
    public:

    static int k;
    };

    int XYZ::k = 4;


    b83503104, see my other post for clarification.



    -JKop
    JKop, May 24, 2004
    #5
  6. b83503104

    JKop Guest

    JKop posted:

    > class XYZ
    > {
    > public:
    >
    > const unsigned int chocolate;
    >
    > XYZ(void) : chocolate(53)
    > {
    >
    > }
    >
    > };



    You may very well wonder why the hell one would declare a member variable
    const, as opposed to static. Here goes:


    class XYZ
    {
    public:

    const unsigned int chocolate;

    XYZ(const unsigned int icecream) : chocolate(icecream)
    {

    }
    };


    You get one chance at setting the const variable, and that's at the
    constructor.



    -JKop
    JKop, May 24, 2004
    #6
  7. >
    >
    > Incorrect.
    >
    >
    > Even with a static variable you must do the following:
    >
    >
    > class XYZ
    > {
    > public:
    >
    > static int k;
    > };
    >
    > int XYZ::k = 4;
    >
    >


    Your information is out of date. In class initialisation of static const
    integers was added to C++ during the standardisation process.

    > b83503104, see my other post for clarification.
    >


    The following compiles, links and runs with VC++ 7.1 and gcc 3.3.1

    #include <iostream>
    using namespace std;

    class XYZ
    {
    public:
    static const int k = 4;
    };

    const int XYZ::k;

    int main()
    {
    cout << XYZ::k << '\n';
    }

    john
    John Harrison, May 24, 2004
    #7
  8. b83503104

    JKop Guest

    John Harrison posted:

    > Your information is out of date. In class initialisation of static const
    > integers was added to C++ during the standardisation process.



    I apologize, sorry, I was unaware.


    >> b83503104, see my other post for clarification.
    >>

    >
    > The following compiles, links and runs with VC++ 7.1 and gcc 3.3.1
    >
    > #include <iostream>
    > using namespace std;
    >
    > class XYZ
    > {
    > public:
    > static const int k = 4;
    > };
    >
    > const int XYZ::k;
    >
    > int main()
    > {
    > cout << XYZ::k << '\n';
    > }



    The above compiles for me! Happy Days!


    -JKop
    JKop, May 24, 2004
    #8
  9. > >>
    > >
    > > The following compiles, links and runs with VC++ 7.1 and gcc 3.3.1
    > >
    > > #include <iostream>
    > > using namespace std;
    > >
    > > class XYZ
    > > {
    > > public:
    > > static const int k = 4;
    > > };
    > >
    > > const int XYZ::k;
    > >
    > > int main()
    > > {
    > > cout << XYZ::k << '\n';
    > > }

    >
    >
    > The above compiles for me! Happy Days!
    >


    The question is does it compile without

    const int XYZ::k;

    Strictly speaking that is required but many compilers allow you to omit it.
    Both MSVC++ 7.1 and gcc 3.3.1 do in the code above. But change

    cout << XYZ::k << '\n';

    to

    cout << &XYZ::k << '\n';

    and still omitting 'const int XYZ::k' and gcc gives a link error but VC++
    still accepts it. Not sure how it manages to print the address of something
    that doesn't exist.

    john
    John Harrison, May 24, 2004
    #9
  10. b83503104

    Jeff Schwab Guest

    Re: How to define a constant integer inside a class with keywordconst ?

    JKop wrote:
    > Jeff Schwab posted:
    >
    >
    >>b83503104 wrote:
    >>
    >>>I know this is illegal:
    >>>class XYZ {
    >>>const int myConst = 1;
    >>>...};
    >>>
    >>>But then, does it mean I have to use #define ?

    >>
    >>No, you don't have to use #define to define a constant in C++.
    >>

    >
    >
    >
    > Not very helpful. Quite ignorant actually. Asshole.


    ??? What did I say that offended you? I really was trying to answer
    the OP's question.
    Jeff Schwab, May 25, 2004
    #10
  11. b83503104

    JKop Guest

    Jeff Schwab posted:

    > JKop wrote:
    >> Jeff Schwab posted:
    >>
    >>
    >>>b83503104 wrote:
    >>>
    >>>>I know this is illegal:
    >>>>class XYZ {
    >>>>const int myConst = 1; ...};
    >>>>
    >>>>But then, does it mean I have to use #define ?
    >>>
    >>>No, you don't have to use #define to define a constant in C++.
    >>>

    >>
    >>
    >>
    >> Not very helpful. Quite ignorant actually. Asshole.

    >
    > ??? What did I say that offended you? I really was trying to answer
    > the OP's question.
    >



    Sorry!! I misinterpreted what you said. I've read a few posts today where
    people have just given stupid answers just like "Nope.", particularly
    Stephen Waits, and I was thinking along those lines when I read *your* post.

    Sorry again for the misunderstanding.


    -JKop
    JKop, May 25, 2004
    #11
    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.

Share This Page