Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to define a constant integer inside a class with keyword const ?

Reply
Thread Tools

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

 
 
b83503104
Guest
Posts: n/a
 
      05-24-2004
I know this is illegal:
class XYZ {
const int myConst = 1;
....};

But then, does it mean I have to use #define ?
Thanks
 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      05-24-2004
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++.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004

"b83503104" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      05-24-2004
John Harrison posted:

>
> "b83503104" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004
>
>
> 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


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004
> >>
> >
> > 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


 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      05-24-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to define operator "const char *" of enumerated type defined inside a class Bill Davy C++ 6 09-25-2008 09:28 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 3 09-06-2006 02:58 PM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments