如何用关键字const在类中定义一个常量整数? [英] How to define a constant integer inside a class with keyword const ?
问题描述
我知道这是非法的:
类XYZ {
const int myConst = 1;
....};
但是,这是否意味着我必须使用#define?
谢谢
I know this is illegal:
class XYZ {
const int myConst = 1;
....};
But then, does it mean I have to use #define ?
Thanks
推荐答案
b83503104写道:
b83503104 wrote:
我知道这是非法的:
类XYZ {
const int myConst = 1;
...};
但是,这是否意味着我必须使用#define?
I know this is illegal:
class XYZ {
const int myConst = 1;
...};
But then, does it mean I have to use #define ?
不,你不必使用#define来定义一个常量C ++。
No, you don''t have to use #define to define a constant in C++.
" b83503104" < B8 ******* @ yahoo.com>在消息中写道
news:73 ************************** @ posting.google.c om ...
"b83503104" <b8*******@yahoo.com> wrote in message
news:73**************************@posting.google.c om...
我知道这是非法的:
类XYZ {
const int myConst = 1;
...};
然后,这是否意味着我必须使用#define?
谢谢
I know this is illegal:
class XYZ {
const int myConst = 1;
...};
But then, does it mean I have to use #define ?
Thanks
类XYZ {
static const int myConst = 1;
...};
现在它是合法的。你也可以考虑
类XYZ {
enum {myConst = 1};
...};
不要使用#define,无论你做什么。除了别的什么#defines
从不在课堂上。
john
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
Jeff Schwab发表:
Jeff Schwab posted:
b83503104写道:
b83503104 wrote:
我知道这是非法的:
类XYZ {
const int myConst = 1;
I know this is illegal:
class XYZ {
const int myConst = 1;
...};
But then, does it mean I have to use #define ?
不,你不必使用#define用C ++定义一个常量。
No, you don''t have to use #define to define a constant in C++.
不是很有用。实际上很无知。混蛋。
这就是它的完成方式:
class XYZ
{
public:
const unsigned int chocolate;
XYZ(无效):巧克力(53)
{
}
};
如果它是一个静态变量,它就是这样完成的:
类XYZ
{
public
static const unsigned int chocolate;
XYZ(无效)
{
}
};
const unsigned int XYZ :: chocolate = 53;
希望有所帮助。
-JKop
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
这篇关于如何用关键字const在类中定义一个常量整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!