Click here to Skip to main content
15,913,465 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Defining Global Constants Pin
Leslie Sanford12-Jan-08 18:14
Leslie Sanford12-Jan-08 18:14 
GeneralRe: Defining Global Constants Pin
Member 75496013-Jan-08 5:53
Member 75496013-Jan-08 5:53 
GeneralRe: Defining Global Constants Pin
Maxwell Chen12-Jan-08 18:03
Maxwell Chen12-Jan-08 18:03 
GeneralRe: Defining Global Constants Pin
Leslie Sanford12-Jan-08 18:13
Leslie Sanford12-Jan-08 18:13 
GeneralRe: Defining Global Constants [modified] Pin
Maxwell Chen13-Jan-08 7:08
Maxwell Chen13-Jan-08 7:08 
GeneralRe: Defining Global Constants Pin
Member 75496013-Jan-08 10:40
Member 75496013-Jan-08 10:40 
GeneralRe: Defining Global Constants Pin
CPallini13-Jan-08 10:51
mveCPallini13-Jan-08 10:51 
GeneralRe: Defining Global Constants Pin
Member 75496013-Jan-08 17:18
Member 75496013-Jan-08 17:18 
I just tested this with the MSVS Team Server (MSVC 9) and, yes, you can define a class constant,

class A
{
	static const int MAXBUFFERLEN = 1024;
	char szBuffer[MAXBUFFERLEN];
public:
	A() {}
	~A() {}
};

which is a Good Thing seeing as I already claimed it. Wink | ;) Which is "better" than declaring it then defining it in the *.cpp file.

Why are the enum less desireable? I would have to seek it out again but what springs to mind is type. Enumerations are types and should be treated as such and not as substitutes for something else. It may not be so but I would expect the compiler to complain if I substituted a enumerated value for an integer,

enum WeekDays
{
	Monday = 0,
	Tuesday = 1,
	Wednesday = 2,
	Thursday = 3,
	Friday = 4,
	Saturday = 5,
	Sunday = 6,
	//NumberofWeekDays = Sunday + 1	// error prone
	NumberofWeekDays = 7	// explicit definition
};

int Workdays[5];

void f()
{
	int i = Workdays[Monday];	// error ??? : integer index expected
	i = Monday;			// error ??? integer value expected	
}

Implicit conversion resolves this but should it? There is also the issue of invalid convesion. What does this mean:

void f(WeekDays aDay) {}

void g()
{
	f(WeekDays(24));	// invalid type coersion
}

When I first learned enumerations I learned never to count on their value. That may be out of date now but it strikes me how many applications will fail if the order or value of the enumerations changes. Very fragile. (Yet, at work we use this technique extensively; because we have to.) As a type, this seems contrary to what is desired. I want consistency and predictability. Monday comes before Tuesday; by how much doesn't matter. I can't multiply Monday by any value and get Friday. I can't make a Weekday from a sow's ear.

YMMV
GeneralRe: Defining Global Constants Pin
Maxwell Chen13-Jan-08 18:13
Maxwell Chen13-Jan-08 18:13 
GeneralRe: Defining Global Constants Pin
Maxwell Chen13-Jan-08 16:18
Maxwell Chen13-Jan-08 16:18 
GeneralRe: Defining Global Constants Pin
Member 75496013-Jan-08 17:19
Member 75496013-Jan-08 17:19 
GeneralRe: Defining Global Constants Pin
Maxwell Chen13-Jan-08 18:02
Maxwell Chen13-Jan-08 18:02 
GeneralRe: Defining Global Constants Pin
Luc Pattyn13-Jan-08 11:13
sitebuilderLuc Pattyn13-Jan-08 11:13 
GeneralOpinion about Notify Icon Pin
Richard Andrew x6412-Jan-08 11:45
professionalRichard Andrew x6412-Jan-08 11:45 
GeneralRe: Opinion about Notify Icon Pin
David Crow12-Jan-08 16:53
David Crow12-Jan-08 16:53 
GeneralRe: Opinion about Notify Icon Pin
Richard Andrew x6413-Jan-08 8:39
professionalRichard Andrew x6413-Jan-08 8:39 
GeneralRe: Opinion about Notify Icon Pin
Mark Salsbery13-Jan-08 8:16
Mark Salsbery13-Jan-08 8:16 
GeneralRe: Opinion about Notify Icon Pin
Richard Andrew x6413-Jan-08 8:40
professionalRichard Andrew x6413-Jan-08 8:40 
QuestionRe: Opinion about Notify Icon Pin
David Crow14-Jan-08 2:34
David Crow14-Jan-08 2:34 
GeneralRe: Opinion about Notify Icon Pin
Mark Salsbery14-Jan-08 6:08
Mark Salsbery14-Jan-08 6:08 
GeneralNewbie Question on C Pointers Pin
_NielsB12-Jan-08 10:55
_NielsB12-Jan-08 10:55 
GeneralRe: Newbie Question on C Pointers Pin
Chris Losinger12-Jan-08 11:03
professionalChris Losinger12-Jan-08 11:03 
GeneralRe: Newbie Question on C Pointers Pin
CPallini12-Jan-08 11:58
mveCPallini12-Jan-08 11:58 
GeneralRe: Newbie Question on C Pointers Pin
_NielsB12-Jan-08 21:29
_NielsB12-Jan-08 21:29 
GeneralRe: Newbie Question on C Pointers Pin
CPallini12-Jan-08 23:19
mveCPallini12-Jan-08 23:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.