473,788 Members | 3,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
10 1869
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

"b83503104" <b8*******@yaho o.com> wrote in message
news:73******** *************** ***@posting.goo gle.com...
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
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
John Harrison posted:

"b83503104" <b8*******@yaho o.com> wrote in message
news:73******** *************** ***@posting.goo gle.com...
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
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(icecr eam)
{

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

-JKop
Jul 22 '05 #6
>

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
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
> >>

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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

97
27822
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
3
1501
by: blueblueblue2005 | last post by:
Hi, what is the different between the following two ways to use const in a function definition: 1. const Array &operator=(const Array &); 2. int getSize() const; keyword const to the left of return type and to the right of the parameter list, what is the different here? I know for the 2nd case, the function can not modify the object's data member.
4
51179
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be a wise idea to create a clsCommonApp and let all other classes to be derived from that class? and define all constants in that base class? Any other suggestions are more than welcome. (BTW this is not for one constant, I have multiple...
7
2563
by: Don Wash | last post by:
Hi There! I'm trying to define constants so that I can refer those constants from any page of my ASP.NET website. I know I can use <appSettings> in web.config XML file but I don't want to parse and read the whole XML document everytime a page is requested. In classic ASP, I used Include files and define constants using Const keyword so that every page that has been included the file can refer the constants.
2
7999
by: Jason | last post by:
What is the difference between 'const' and 'define()' ? When would I prefer using 'const' over 'define', or vice versa? It seems if i do: const secondsInMinute = 60; define("secondsInMinute", 60); Aren't these two the same thing? - Jason
0
3575
by: CHU Run-min | last post by:
The following code was written by me. It is used for evaluating constant expressions. I think it is good. Any criticism? namespace Churunmin.HelloWorld {
1
5225
by: sophia | last post by:
Dear all, the following are the differences b/w #define and typedef ,which i have seen in Peter van der lindens book. is there any other difference between thes two ? The right way to think about typedef as being a complete encapsulated type - you can't add to it after you have declared it.
3
4066
by: longbrmb | last post by:
I'm new to C++/CLI and my main background is Java. I'm trying to create an array of constants as a static member of a class. An example is shown below: public ref class TestClass { public: static const array<constant int>^ stuff = {1,2,3,4,5}; }; This gives me compiler error C4538 which basically says that you can't
8
1669
by: fabian.lim | last post by:
Hi, I have a question on constant variables. In the following code snippet, I have a function assign() that takes in an iterator to the private variable v, the number of stuff to assign (int n), and the information to assign (a const pointer to a class object Vector<TYPE>. According to the arrow below, I put a const keyword in the function. To my knowledge, this means that all private variables in this object
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.