473,705 Members | 4,203 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 1860
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
27785
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
3
1497
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
51178
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
2557
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
7996
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
3573
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
5221
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
4059
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
1663
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
8768
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
8690
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
9139
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...
0
7895
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...
1
6606
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4440
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...
0
4704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2083
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.