473,766 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_COUN T;

static const INT32 MAX_LOGGER_METH OD_COUNT;

static const INT32 MAX_LOG_MESG_CO UNT;

static const INT32 MAX_REG_COUNT;

static const INT32 MAX_FILE_LENGTH ;
};

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

const INT32 FileLogConstant ::REGISTER_LOGG ER = 0x00;

const INT32 FileLogConstant ::REGISTER_METH OD = 0x01;

const INT32 FileLogConstant ::MAX_LOGGER_CO UNT = 64;

const INT32 FileLogConstant ::MAX_LOGGER_ME THOD_COUNT = 128;

const INT32 FileLogConstant ::MAX_LOG_MESG_ COUNT = 128;

const INT32 FileLogConstant ::MAX_REG_COUNT = 16;

const INT32 FileLogConstant ::MAX_FILE_LENG TH = 4 + 4 +
FileLogConstant ::MAX_LOGGER_CO UNT *
(16 + 4 + 64 * FileLogConstant ::MAX_LOGGER_ME THOD_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_LENG TH];
};

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_LENG TH already defined errors.

How to define class final constants?

Nov 28 '06 #1
13 1977
* 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_LOGG ER = 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::co nstant1];
};

Nov 28 '06 #3
On 27 Nov 2006 18:00:23 -0800 in comp.lang.c++, "Allen"
<ch****@naritec h.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****@naritec h.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5562
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
2557
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 INT_CONSTANT 4852 #endif
7
2486
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 constant integer key value. I am wondering what is a good way to define these constants. 1) #define A = 0 #define B = 1 #define C = 2
4
51179
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 a wise idea to create a clsCommonApp and let all other classes to be derived from that class? and define all constants in that base class? Any other suggestions are more than welcome. (BTW this is not for one constant, I have multiple...
7
2562
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 and read the whole XML document everytime a page is requested. In classic ASP, I used Include files and define constants using Const keyword so that every page that has been included the file can refer the constants.
1
2185
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 following should happen: the two Die objects should be rolled, the face value of the two Die objects should be displayed and the label should display the sum of the two dice. I got so close so far, but now the gui won't even pop up with the driver...
5
2574
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: const char *s = string_mapper<thetype>(); However I do not know the string to be associated with the type at compile time, and will need a way to set up the mapping, to be created at run time, possibly like so: void foo(char*...
6
1275
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 mixing @classmethod and __getattr__. (The property approach compiles, but execution says that you can't execute properties.) I've got a rather large number of variables, so I don't want to define function accessors for each of them, and I...
7
2777
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. Regards Chris Saunders
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.