473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to define class final constants?

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
13 1949
* 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
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
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

"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

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

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

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

"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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: codecraig | last post by:
Hi, I have a question about how to define constants. My directory structure looks like... C:\ --> abc.py --> utils --> __init__.py --> CustomThing.py
8
by: John Ratliff | last post by:
Let's say I had a program which uses some constants. Would it be "better" to declare them like this: #ifndef __MY_HDR_FILE #define __MY_HDR_FILE #define STRING_CONST "some string..." #define...
7
by: Morgan Cheng | last post by:
Hi, In my program module, there are some Constants should be defined to be integer key value of std::map. In the module, methods of a few classes will return std::map containing value indexed by...
4
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be...
7
by: Don Wash | last post by:
Hi There! I'm trying to define constants so that I can refer those constants from any page of my ASP.NET website. I know I can use <appSettings> in web.config XML file but I don't want to parse...
1
by: Kburge03 | last post by:
Hi!! I've been working on this assingment for class where I have to design and implement an application that displays two Die objects, a button, and a label. Every time the button is pushed, the...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
6
by: Charles D Hixson | last post by:
I'm trying to construct read-only variables at the class level. I've been unsuccessful. Any suggestions? Mixing @classmethod and @property doesn't appear to produce workable code. Ditto for...
7
by: Chris Saunders | last post by:
I'm not very familiar with C#. I have a static class and would like to define some constants similarily to Math.PI. Could someone show me how to go about doing this. So far no luck searching. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.