By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

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

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

But then, does it mean I have to use #define ?
Thanks
Jul 22 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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++.
Jul 22 '05 #2

P: n/a

"b83503104" <b8*******@yahoo.com> wrote in message
news:73**************************@posting.google.c om...
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
Jul 22 '05 #3

P: n/a
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
Jul 22 '05 #4

P: n/a
John Harrison posted:

"b83503104" <b8*******@yahoo.com> wrote in message
news:73**************************@posting.google.c om...
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
Jul 22 '05 #5

P: n/a
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
Jul 22 '05 #6

P: n/a
>

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
Jul 22 '05 #7

P: n/a
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
Jul 22 '05 #8

P: n/a
> >>

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
Jul 22 '05 #9

P: n/a
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.
Jul 22 '05 #10

P: n/a
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
Jul 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.