在类中创建const? [英] Creating a const in a class?

查看:59
本文介绍了在类中创建const?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello cppers


我正在将Java类转换为C ++并在Java类中使用它:


public static final int APPLICATION = 0x40;


所以在我的C ++类中我尝试过:


const int APPLICATION = 0x40;


当然给我一个C2252编译错误。


如何在C ++类中创建等效类型的常量?

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?

推荐答案



Angus skrev:

Angus skrev:

Hello cppers


我正在将Java类转换为C ++并在Java类中使用它:
Hello cppers

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



哎哟! ;-)

Ouch! ;-)


>

public static final int APPLICATION = 0x40;


所以在我的C ++类中,我尝试过:


const int APPLICATION = 0x40;


这当然给了我一个C2252编译错误。
>
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).


>

如何在C ++类中创建等效类型的常量?
>
How would I create the equivalent type of constant in a C++ class?



如果你使用的是旧的编译器(例如MSVC6),下面的技巧将会是

do:


enum {APPLICATION = 0x40};


/ Peter

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

enum { APPLICATION = 0x40 };

/Peter




Angus写道:

Angus wrote:

Hello cppers


我正在将Java类转换为C ++并将其转换为Java类:


public static final int APPLICATION = 0x40;


所以在我的C ++课程中我尝试过:

const int APPLICATION = 0x40;


这当然给我一个C2252编译错误。


我如何创建C ++类中的等价类型的常量?
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?



我假设您正在使用其中一个MS编译器,因为您列出了

编译错误。你有没有检查过这个

编译器错误的MSDN帮助?有一个页面为您提供解决方案:
http://支持.microsoft.com / kb / 241569

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




" peter koch" < pe *************** @ gmail.com写信息

新闻:11 *************** *******@a3g2000cwd.googlegro ups.com ...

"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@a3g2000cwd.googlegro ups.com...

>

Angus skrev:
>
Angus skrev:

Hello cppers


我正在将Java类转换为C ++并在Java类中使用它:
Hello cppers

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



哎哟! ;-)

Ouch! ;-)



public static final int APPLICATION = 0x40;


所以在我的C ++类中我尝试过:


const int APPLICATION = 0x40;


这当然给了我一个C2252编译错误。

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



如何在C ++类中创建等效类型的常量?

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



如果您使用的是旧的编译器(例如MSVC6),以下技巧将会是




enum {APPLICATION = 0x40};


/ Peter


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.


这篇关于在类中创建const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆