473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Creating a const in a class?

Hello cppers

I am converting a Java class to C++ and have this in a Java class:

public static final int APPLICATION = 0x40;

So in my C++ class I tried:

const int APPLICATION = 0x40;

which of course gives me a C2252 compile error.

How would I create the equivalent type of constant in a C++ class?
Jan 3 '07 #1
10 1563

Angus skrev:
Hello cppers

I am converting a Java class to C++ and have this in a Java class:
Ouch! ;-)
>
public static final int APPLICATION = 0x40;

So in my C++ class I tried:

const int APPLICATION = 0x40;

which of course gives me a C2252 compile error.
Why of course? The above declaration looks fine (but ugly: reserve all
uppercase for macroes).
>
How would I create the equivalent type of constant in a C++ class?
If you're using an old compiler (e.g. MSVC6), the following trick will
do:

enum { APPLICATION = 0x40 };

/Peter

Jan 3 '07 #2

Angus wrote:
Hello cppers

I am converting a Java class to C++ and have this in a Java class:

public static final int APPLICATION = 0x40;

So in my C++ class I tried:

const int APPLICATION = 0x40;

which of course gives me a C2252 compile error.

How would I create the equivalent type of constant in a C++ class?
I'm assuming you're using one of the MS compilers, because of the
compile error you listed. Did you check the MSDN help about this
compiler error? There's a page with a solution for you:
http://support.microsoft.com/kb/241569

Jan 3 '07 #3

"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@a3g2000cwd.googlegro ups.com...
>
Angus skrev:
Hello cppers

I am converting a Java class to C++ and have this in a Java class:
Ouch! ;-)

public static final int APPLICATION = 0x40;

So in my C++ class I tried:

const int APPLICATION = 0x40;

which of course gives me a C2252 compile error.

Why of course? The above declaration looks fine (but ugly: reserve all
uppercase for macroes).

How would I create the equivalent type of constant in a C++ class?

If you're using an old compiler (e.g. MSVC6), the following trick will
do:

enum { APPLICATION = 0x40 };

/Peter
Thanks. I assumed it was a language issue not a compiler limitation issue.

Jan 3 '07 #4
Angus wrote:
[...]
I assumed it was a language issue not a compiler limitation
issue.
You should never assume anything about an old compiler, which
actually predates the language Standard. And why are you using
such an old compiler, anyway? And what book on C++ are you
reading that doesn't describe class-wide integral constants?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 3 '07 #5
Angus wrote:
Hello cppers

I am converting a Java class to C++ and have this in a Java class:

public static final int APPLICATION = 0x40;

So in my C++ class I tried:

const int APPLICATION = 0x40;
If you're trying to define a const inline in a class definition, it must
be a *static* const member of intergral or enum type.

i.e.

static const int APPLICATION = 0x40;

Jan 3 '07 #6
On Wed, 03 Jan 2007 15:14:22 +0000, Angus wrote:
Hello cppers

I am converting a Java class to C++ and have this in a Java class:

public static final int APPLICATION = 0x40;

So in my C++ class I tried:

const int APPLICATION = 0x40;

which of course gives me a C2252 compile error.

How would I create the equivalent type of constant in a C++ class?
Don't forget to define a static const int by writing:

const int my_clas::APPLICATION;
Jan 3 '07 #7
Philipp Reh wrote:
On Wed, 03 Jan 2007 15:14:22 +0000, Angus wrote:
>Hello cppers

I am converting a Java class to C++ and have this in a Java class:

public static final int APPLICATION = 0x40;

So in my C++ class I tried:

const int APPLICATION = 0x40;

which of course gives me a C2252 compile error.

How would I create the equivalent type of constant in a C++ class?

Don't forget to define a static const int by writing:

const int my_clas::APPLICATION;
That's only needed if it's used as an lvalue (e.g. its address is
taken).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 3 '07 #8
From C++ FAQs book

class Fred {
public:
Fred(int i) throw();
Fred(int i, int j) throw();
protected:
int i_;
const int j_;
};

Fred::Fred(int i) throw(): i_(i), j_(10)
<-- 1
{ }

Fred::Fred(int i, int j) throw(): i_(i), j_(j)
<-- 2
{ }

int main()
{
Fred a(5); //a.j_ will always be 10
Fred b(5,15); //b.j_ will always be 15
Fred c(5,20); //c.j_ will always be 20
}

"Angus" <no****@gmail.comwrote in message
news:en*******************@news.demon.co.uk...
Hello cppers

I am converting a Java class to C++ and have this in a Java class:

public static final int APPLICATION = 0x40;

So in my C++ class I tried:

const int APPLICATION = 0x40;

which of course gives me a C2252 compile error.

How would I create the equivalent type of constant in a C++ class?


Jan 3 '07 #9


On Jan 3, 4:14 pm, "Angus" <nos...@gmail.comwrote:
Hello cppers

I am converting a Java class to C++ and have this in a Java class:

public static final int APPLICATION = 0x40;

So in my C++ class I tried:

const int APPLICATION = 0x40;

which of course gives me a C2252 compile error.

How would I create the equivalent type of constant in a C++ class?
just type static int APPLICATION = 0x40

Jan 3 '07 #10
This is what I normally do. Although the enum trick saves typing.

//IN HEADER FILE
class Example {
private:
static const int COLD_DAYS; //This could be public or private

public:
void printColdDays();
}

//IN IMPLEMENTATION (.cpp file)

#include <iostream>
const int Example::COLD_DAYS = 79;

void Example::printColdDays() {
std::cout << "There are " << COLD_DAYS << " cold days." << endl;
}

//IN MAIN FUNCTION
Example badName;
badName.printColdDays();

Jan 4 '07 #11

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

Similar topics

6
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
7
by: Joe Van Dyk | last post by:
Hi, Say I have: class Latitude { friend bool operator==(const Latitude& lhs, const Latitude& rhs); friend bool operator<(const Latitude& lhs, const Latitude& rhs); friend std::ostream&...
7
by: nono909 | last post by:
I wrote the following time class for the following assignment.i need help in completing this program pleasee. Write a class to hold time. Time is the hour, minute and seconds. Write a constructor...
1
by: girays | last post by:
I have a template class which name is EntityRepository and when I compile this class I get no error. But when I use this class in a main method I get LNK2019 linking error. std::map object is used...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
10
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
2
by: FFrozTT | last post by:
I am having a problem creating a DLL with an entry point. I've been trying sub Main, DllMain, and I get nothing. When I run dumpbin - exports mydll.dll I see no entry points, also the dll when...
3
by: alan | last post by:
Hello all, I'd like to know if there is a nice method of defining a functor creator which accepts an N-ary function and returns a functor based on that function. For example I have a function:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...

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.