How to define class final constants?

A

Allen

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?
 
A

Alf P. Steinbach

* 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

Allen

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];
};
 
D

David Harmon

On 27 Nov 2006 18:00:23 -0800 in comp.lang.c++, "Allen"
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.
 
A

Allen

"David Harmon дµÀ£º

Do you have sample codes?
I tried but failed.
The compiler tells two kinds of errors, redefinition and non-static
dimension.
 
B

BobR

Allen wrote in message ...

/* """
"David Harmon дµÀ£º

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

Allen

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

David Harmon

On 29 Nov 2006 00:16:36 -0800 in comp.lang.c++, "Allen"
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();
};
 
K

kwikius

Allen said:
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
 
A

Allen

No. It cannot be compiled by VC++ 6.0.
I will test with gcc.

"kwikius дµÀ£º
"
Allen said:
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
 
K

kwikius

Allen said:
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
 
A

Allen

"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.
 
K

kwikius

Allen said:
"kwikius дµÀ£º
"

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
 
B

BobR

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];
// .....
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top