By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,836 Members | 2,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,836 IT Pros & Developers. It's quick & easy.

How to define class final constants?

P: n/a
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?

Nov 28 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
* Allen:
I defines FileLogConstant class as following.

class FileLogConstant
{
public:
static const INT32 REGISTER_LOGGER;
Don't use all uppercase except for macros (which should all be all
uppercase).

const INT32 FileLogConstant::REGISTER_LOGGER = 0x00;
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?
Nov 28 '06 #2

P: n/a
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];
};

Nov 28 '06 #3

P: n/a
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.

Nov 28 '06 #4

P: n/a

"David Harmon 写道:
>
Yes.
Do you have sample codes?
I tried but failed.
The compiler tells two kinds of errors, redefinition and non-static
dimension.

Nov 29 '06 #5

P: n/a

Allen wrote in message ...

/* """
"David Harmon 写道:
>
Yes.
Do you have sample codes?
I tried but failed.
The compiler tells two kinds of errors, redefinition and non-static
dimension.

""" */

class Allen{
static const size_t Max = 5;
public:
Allen();
};
Nov 29 '06 #6

P: n/a
>
class Allen{
static const size_t Max = 5;
public:
Allen();
};
This code cannot be compiled.

What I want is class member constant used directly by array dimension
declaration:

class Allen{
static const size_t Max;
static char buffer[Max]; // Max cannot be used here. I do want.
public:
Allen();
};

Nov 29 '06 #7

P: n/a
On 29 Nov 2006 00:16:36 -0800 in comp.lang.c++, "Allen"
<ch****@naritech.cnwrote,
>What I want is class member constant used directly by array dimension
declaration:

class Allen{
static const size_t Max;
static const size_t Max = 73;
static char buffer[Max]; // Max cannot be used here. I do want.
public:
Allen();
};
Nov 29 '06 #8

P: n/a

Allen wrote:

class Allen{
static const size_t Max = 5;
public:
Allen();
};

This code cannot be compiled.
? It should compile OK AFAICS :

#include <iostream>

// static integral constants can be initialised inline:
class Allen{
static const size_t Max = 5; //fine
public:
Allen(){}
};

class Allen1{
static const size_t Max = 5; //fine

public:
static char buffer[Max]; // declaration fine
Allen1(){}
};

char Allen1::buffer[] = "fine"; // member definition in cpp file

int main()
{
std::cout << Allen1::buffer <<'\n';
}

regards
Andy Little

Nov 29 '06 #9

P: n/a
No. It cannot be compiled by VC++ 6.0.
I will test with gcc.

"kwikius 写道:
"
Allen wrote:
>
class Allen{
static const size_t Max = 5;
public:
Allen();
};
This code cannot be compiled.

? It should compile OK AFAICS :

#include <iostream>

// static integral constants can be initialised inline:
class Allen{
static const size_t Max = 5; //fine
public:
Allen(){}
};

class Allen1{
static const size_t Max = 5; //fine

public:
static char buffer[Max]; // declaration fine
Allen1(){}
};

char Allen1::buffer[] = "fine"; // member definition in cpp file

int main()
{
std::cout << Allen1::buffer <<'\n';
}

regards
Andy Little
Nov 30 '06 #10

P: n/a

Allen wrote:
No. It cannot be compiled by VC++ 6.0.
VC6 has a lot of problems with implementing the C++ standard correctly,
IOW its probably the compiler in this case that is letting you down.
I will test with gcc.
If it fails with gcc then again try a more recent version if possible,
but if it does fail then you probably have a quite old version of gcc.

regards
Andy Little

Nov 30 '06 #11

P: n/a

"kwikius 写道:
"
Allen wrote:

VC6 has a lot of problems with implementing the C++ standard correctly,
IOW its probably the compiler in this case that is letting you down.
If it fails with gcc then again try a more recent version if possible,
but if it does fail then you probably have a quite old version of gcc.
When was the newest C++ standard put forward?
VC6 may be implement C++ standard before 1998.

Nov 30 '06 #12

P: n/a

Allen wrote:
"kwikius 写道:
"
Allen wrote:

VC6 has a lot of problems with implementing the C++ standard correctly,
IOW its probably the compiler in this case that is letting you down.
If it fails with gcc then again try a more recent version if possible,
but if it does fail then you probably have a quite old version of gcc.

When was the newest C++ standard put forward?
VC6 may be implement C++ standard before 1998.
According to my copy of the C++ standard, BS ISO/IEC 14882 (the C++
standard) was ratified in 1997, however AFAIK Microsoft Borland and
others saw the potential of C++ for Windows development way before that
of course and so produced compilers predicting I guess what the
standard would be and also perhaps more importantly trying to keep
compatibility with their previous software. Also the C++ templates
specification was rushed into the standard quite late as far as I know,
which of course put existing compilers in a tricky position, IOW you
can't blame them for that and MSVC1.0 was my first C++ compiler which I
am really grateful to for introducing me to the language.

As I understand it the template specs were put in the standard without
anyone really having a working compiler that implemented them
correctly. It seems that Bjarne Stroustrup must have cast some sort of
spell over the Approval committee, which even he was surprised actually
worked if I remember from reading his book '"The design and evolution
of C++". So this is the sort of problem compiler writers faced at that
time.

Also I think that the ability to define static integral constants
inside the class definition was another late addition.

For the MS series both VC7.1 (not VC7.0) and VC8.0 do a good job of
implementing the current Standard.

As for gcc I curreently have gcc 4.0 , for earlier versions I am not so
sure, but a wild guess is to try to get a version after around gcc 3.3
or so.

And even in the latest compilers, if you look hard enough, you can find
something that is not correct.

That is part of the mystique of C++ FAICS. No one will ever truly
master it. And of course as soon as compilers get up to speed there
will be a new version of the standard and the whole rigmarole of non
conforming compilers will continue as usual :-)

regards
Andy Little

Nov 30 '06 #13

P: n/a

Allen wrote in message ...
/* """
"kwikius 写道:
"
Allen wrote:

VC6 has a lot of problems with implementing the C++ standard correctly,
IOW its probably the compiler in this case that is letting you down.

If it fails with gcc then again try a more recent version if possible,
but if it does fail then you probably have a quite old version of gcc.
When was the newest C++ standard put forward?
VC6 may be implement C++ standard before 1998.

""" */

Ah ha, old compiler. Then maybe you can use the old hack:

class Allen{
enum { size = 1000 };
int i[size];
// .....
};

--
Bob R
POVrookie
Nov 30 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.