Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating a const in a class?

Angus
Guest
 
Posts: n/a
#1: Jan 3 '07
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?



peter koch
Guest
 
Posts: n/a
#2: Jan 3 '07

re: Creating a const in a class?



Angus skrev:
Quote:
Hello cppers
>
I am converting a Java class to C++ and have this in a Java class:
Ouch! ;-)
Quote:
>
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).
Quote:
>
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

jjds101@yahoo.com
Guest
 
Posts: n/a
#3: Jan 3 '07

re: Creating a const in a class?



Angus wrote:
Quote:
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

Angus
Guest
 
Posts: n/a
#4: Jan 3 '07

re: Creating a const in a class?



"peter koch" <peter.koch.larsen@gmail.comwrote in message
news:1167837650.850553.269740@a3g2000cwd.googlegro ups.com...
Quote:
>
Angus skrev:
Quote:
Hello cppers

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

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).
Quote:

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.



Victor Bazarov
Guest
 
Posts: n/a
#5: Jan 3 '07

re: Creating a const in a class?


Angus wrote:
Quote:
[...]
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


red floyd
Guest
 
Posts: n/a
#6: Jan 3 '07

re: Creating a const in a class?


Angus wrote:
Quote:
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;

Philipp Reh
Guest
 
Posts: n/a
#7: Jan 3 '07

re: Creating a const in a class?


On Wed, 03 Jan 2007 15:14:22 +0000, Angus wrote:
Quote:
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;
Victor Bazarov
Guest
 
Posts: n/a
#8: Jan 3 '07

re: Creating a const in a class?


Philipp Reh wrote:
Quote:
On Wed, 03 Jan 2007 15:14:22 +0000, Angus wrote:
>
Quote:
>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


Mari
Guest
 
Posts: n/a
#9: Jan 3 '07

re: Creating a const in a class?


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" <nospam@gmail.comwrote in message
news:engh8l$84p$1$8300dec7@news.demon.co.uk...
Quote:
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?
>
>

miki.tadija@gmail.com
Guest
 
Posts: n/a
#10: Jan 3 '07

re: Creating a const in a class?




On Jan 3, 4:14 pm, "Angus" <nos...@gmail.comwrote:
Quote:
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

Tobias
Guest
 
Posts: n/a
#11: Jan 4 '07

re: Creating a const in a class?


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();

Closed Thread