如何定义类的最终常量? [英] How to define class final constants?

查看:91
本文介绍了如何定义类的最终常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将FileLogConstant类定义如下。


class FileLogConstant

{

public:

static const INT32 REGISTER_LOGGER;


static const INT32 REGISTER_METHOD;


static const INT32 MAX_LOGGER_COUNT;


static const INT32 MAX_LOGGER_METHOD_COUNT;


static const INT32 MAX_LOG_MESG_COUNT;


static const INT32 MAX_REG_COUNT;


static const INT32 MAX_FILE_LENGTH;

};


//////////////////// ///////////////////////// BLOCK

DEF /////////////// ///////////////////////////////////////////////

const INT32 FileLogConstant :: REGISTER_LOGGER = 0x00;


const INT32 FileLogConstant :: REGISTER_METHOD = 0x01;


const INT32 FileLogConstant :: MAX_LOGGER_COUNT = 64;


const INT32 FileLogConstant :: MAX_LOGGER_METHOD_COUNT = 128;


const INT32 FileLogConstant :: M AX_LOG_MESG_COUNT = 128;


const INT32 FileLogConstant :: MAX_REG_COUNT = 16;


const INT32 FileLogConstant :: MAX_FILE_LENGTH = 4 + 4 +

FileLogConstant :: MAX_LOGGER_COUNT *

(16 + 4 + 64 * FileLogConstant :: MAX_LOGGER_METHOD_COUNT)+ 4 + 4 +

FileLogConstant :: MAX_LOG_MESG_COUNT *( 2 + 2 + 4 + 4 + 256)+

FileLogConstant :: MAX_REG_COUNT *(2 + 2 + 64);


////// /////////////////////////////////////////// BLOCK

DEF /////////////////////////////////////////////// /////////////


class Foo

{

public:

char buffer [FileLogConstant :: MAX_FILE_LENGTH];

};


当BLOCK DEF放入FileLogConstant.cpp文件时,缓冲区

类Foo的声明将被编译出错。当BLOCK

DEF放在FileLogConstant.h文件中时,编译器将生成许多

const FileLogConstant :: MAX_FILE_LENGTH已经定义的错误。


如何定义类的最终常量?

I defines FileLogConstant class as following.

class FileLogConstant
{
public:
static const INT32 REGISTER_LOGGER;

static const INT32 REGISTER_METHOD;

static const INT32 MAX_LOGGER_COUNT;

static const INT32 MAX_LOGGER_METHOD_COUNT;

static const INT32 MAX_LOG_MESG_COUNT;

static const INT32 MAX_REG_COUNT;

static const INT32 MAX_FILE_LENGTH;
};

/////////////////////////////////////////////BLOCK
DEF//////////////////////////////////////////////////////////////

const INT32 FileLogConstant::REGISTER_LOGGER = 0x00;

const INT32 FileLogConstant::REGISTER_METHOD = 0x01;

const INT32 FileLogConstant::MAX_LOGGER_COUNT = 64;

const INT32 FileLogConstant::MAX_LOGGER_METHOD_COUNT = 128;

const INT32 FileLogConstant::MAX_LOG_MESG_COUNT = 128;

const INT32 FileLogConstant::MAX_REG_COUNT = 16;

const INT32 FileLogConstant::MAX_FILE_LENGTH = 4 + 4 +
FileLogConstant::MAX_LOGGER_COUNT *
(16 + 4 + 64 * FileLogConstant::MAX_LOGGER_METHOD_COUNT) + 4 + 4 +
FileLogConstant::MAX_LOG_MESG_COUNT * (2 + 2 + 4 + 4 + 256) +
FileLogConstant::MAX_REG_COUNT * (2 + 2 + 64);

/////////////////////////////////////////////////BLOCK
DEF////////////////////////////////////////////////////////////

class Foo
{
public:
char buffer[FileLogConstant::MAX_FILE_LENGTH];
};

when BLOCK DEF is put in FileLogConstant.cpp file, the buffer
declaration of class Foo will be compiled with an error. While BLOCK
DEF is put in FileLogConstant.h file, the compiler will generate many
const FileLogConstant::MAX_FILE_LENGTH already defined errors.

How to define class final constants?

推荐答案

* Allen:
* Allen:

我将FileLogConstant类定义如下。


class FileLogConstant

{

public:

static const INT32 REGISTER_LOGGER;
I defines FileLogConstant class as following.

class FileLogConstant
{
public:
static const INT32 REGISTER_LOGGER;



不要使用全部大写,除了宏(应该都是全部

大写)。


const INT32 FileLogConstant :: REGISTER_LOGGER = 0x00;
const INT32 FileLogConstant::REGISTER_LOGGER = 0x00;



除了全部大写名称(不要),这在技术上还可以。但我怀疑你已经将这个定义放在一个包含

不止一次的文件中。如果它在同一翻译中不止一次包含

单位,那么编译器会抱怨,否则,如果它包含在

多个翻译中单位链接器会抱怨,因为你是
然后违反了一个定义规则。


-

A:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门帖子。

问:usenet和电子邮件中最烦人的是什么?

Apart from the all uppercase name (don''t) this is technically OK. But I
suspect that you have placed this definition in a file that you include
more than once. If it''s included more than once in the same translation
unit then the compiler will complain, and otherwise, if it''s included in
more than one translation unit the linker will complain, because you''re
then violating the One Definition Rule.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


谢谢。


我想要用类成员替换宏常量定义

常量。但是

这些成员常量可以直接用在

类成员的数组维上

声明吗? ie


class Foo

{

public:

char buffer [ConstantCls :: constant1] ;

};

Thank you.

I want to replace macro constant definitions with class member
constants. But
can these member constants directly used in the array dimension of
class member
declaration? i.e.

class Foo
{
public:
char buffer[ConstantCls::constant1];
};


2006年11月27日18:00:23 -0800 in comp.lang.c ++," Allen"

< ch **** @ naritech.cnwrote,
On 27 Nov 2006 18:00:23 -0800 in comp.lang.c++, "Allen"
<ch****@naritech.cnwrote,

>我想用类成员替换宏常量定义
常数。但是
可以将这些成员常量直接用在数组维度的类成员
声明中吗?
>I want to replace macro constant definitions with class member
constants. But
can these member constants directly used in the array dimension of
class member
declaration?



是。

Yes.


这篇关于如何定义类的最终常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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