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

B

b83503104

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

But then, does it mean I have to use #define ?
Thanks
 
J

Jeff Schwab

b83503104 said:
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++.
 
J

John Harrison

b83503104 said:
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
 
J

JKop

Jeff Schwab posted:
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
 
J

JKop

John Harrison posted:
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
 
J

JKop

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
 
J

John Harrison

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
 
J

JKop

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.

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
 
J

John Harrison

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
 
J

Jeff Schwab

JKop said:
Jeff Schwab posted:





Not very helpful. Quite ignorant actually. Asshole.

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

JKop

Jeff Schwab posted:
??? 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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top