Click here to Skip to main content
15,897,704 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Getting forward from console applications to gui Pin
Marco Bertschi15-Sep-13 10:41
protectorMarco Bertschi15-Sep-13 10:41 
QuestionProblem with const struct initialization in a class Pin
Marco Bertschi13-Sep-13 2:47
protectorMarco Bertschi13-Sep-13 2:47 
QuestionRe: Problem with const struct initialization in a class Pin
David Crow13-Sep-13 9:52
David Crow13-Sep-13 9:52 
AnswerRe: Problem with const struct initialization in a class Pin
Marco Bertschi13-Sep-13 10:02
protectorMarco Bertschi13-Sep-13 10:02 
AnswerRe: Problem with const struct initialization in a class Pin
Kosta Cherry13-Sep-13 16:17
Kosta Cherry13-Sep-13 16:17 
AnswerRe: Problem with const struct initialization in a class Pin
Erudite_Eric14-Sep-13 21:43
Erudite_Eric14-Sep-13 21:43 
AnswerRe: Problem with const struct initialization in a class Pin
Marco Bertschi15-Sep-13 10:35
protectorMarco Bertschi15-Sep-13 10:35 
AnswerRe: Problem with const struct initialization in a class Pin
Stefan_Lang17-Sep-13 0:34
Stefan_Lang17-Sep-13 0:34 
There are many solutions for your problem if you don't insist on trying to initialize a struct.

1. Write a function. Apparently your purpose is to check a value range, so all you really need is a function:
C++
/**
 * check if a given value is within the range
 * @return -1 if the value is too low, 0 if the value is in range, 1 if the value is too high
 * @param  value  the value you want to check
 */
int inVersionRange(char value);

That's all you need in a header. The implementation file then can contain the actual constants:
C++
const char Min_Version = -5;
const char max_version = 1;

int inVersionRange(char value) {
   int result = 0;
   if (value < Min_Version)
      result = -1;
   if (value > Max_Version)
      result = 1;
   return result;
}

If you need more functions that access these boundaries, put the implementation into the same file.

2. static const
C++
struct VersionRange {
   static const char VMIN;
   static const char VMAX;
};
// initialize in implementation file like this:
const char VersionRange::VMIN = -5;
const char VersionRange::VMAX = 1;


3. define the values within a namespace instead of a struct
(same as above, but you don't need the static keyword)

4. create a singleton class to hold the constants and make them write-only
Similar to 2, but you create an instance of your class (or struct) which is made 'static constant' through programming concepts rather than language keywords.

5. use const references and an array intializer
This comes back to your original idea of using an initializer:
C++
// header file
class VersionBoundary {
   static const char* const boundaries;
public:
   static const char& VMIN;
   static const char& VMAX;
}
// implementation file
const char* const VersionBoundary::boundaries = { -5, 1 };
const char& VersionBoundary::VMIN = *(boundaries+0); // reference to first value
const char& VersionBoundary::VMAX = *(boundaries+1); // reference to second value

Questionlistview with tooltip Pin
JoneLe8612-Sep-13 18:16
JoneLe8612-Sep-13 18:16 
AnswerRe: listview with tooltip Pin
Richard MacCutchan13-Sep-13 3:35
mveRichard MacCutchan13-Sep-13 3:35 
Questiongetting pointer to document from OnGetTabToolTip in MDI tabbed app Pin
Member 258447412-Sep-13 6:41
Member 258447412-Sep-13 6:41 
SuggestionRe: getting pointer to document from OnGetTabToolTip in MDI tabbed app Pin
David Crow12-Sep-13 16:10
David Crow12-Sep-13 16:10 
GeneralRe: getting pointer to document from OnGetTabToolTip in MDI tabbed app Pin
Member 258447412-Sep-13 18:01
Member 258447412-Sep-13 18:01 
GeneralRe: getting pointer to document from OnGetTabToolTip in MDI tabbed app Pin
Member 258447412-Sep-13 18:04
Member 258447412-Sep-13 18:04 
AnswerRe: getting pointer to document from OnGetTabToolTip in MDI tabbed app Pin
jeron113-Sep-13 5:20
jeron113-Sep-13 5:20 
GeneralRe: getting pointer to document from OnGetTabToolTip in MDI tabbed app Pin
Member 258447413-Sep-13 7:39
Member 258447413-Sep-13 7:39 
QuestionResizing Controls and Texts Pin
Don Guy11-Sep-13 14:00
Don Guy11-Sep-13 14:00 
AnswerRe: Resizing Controls and Texts Pin
digitalspace.xjtu11-Sep-13 15:02
digitalspace.xjtu11-Sep-13 15:02 
AnswerRe: Resizing Controls and Texts Pin
SoMad11-Sep-13 17:16
professionalSoMad11-Sep-13 17:16 
QuestionWhat is doscreatemutexsem Pin
Carl Cioffi11-Sep-13 9:57
Carl Cioffi11-Sep-13 9:57 
QuestionRe: What is doscreatemutexsem Pin
David Crow11-Sep-13 10:33
David Crow11-Sep-13 10:33 
AnswerRe: What is doscreatemutexsem Pin
Carl Cioffi12-Sep-13 3:20
Carl Cioffi12-Sep-13 3:20 
GeneralRe: What is doscreatemutexsem Pin
David Crow12-Sep-13 3:36
David Crow12-Sep-13 3:36 
GeneralRe: What is doscreatemutexsem Pin
Carl Cioffi12-Sep-13 3:55
Carl Cioffi12-Sep-13 3:55 
QuestionRun MFC Program Without Dialog Showing Pin
Andraw11110-Sep-13 6:53
Andraw11110-Sep-13 6:53 

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.