Click here to Skip to main content
15,889,116 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Desiging in C Pin
Albert Holguin20-Jan-15 6:52
professionalAlbert Holguin20-Jan-15 6:52 
SuggestionRe: Desiging in C Pin
David Crow20-Jan-15 4:06
David Crow20-Jan-15 4:06 
AnswerRe: Desiging in C Pin
Richard MacCutchan20-Jan-15 5:42
mveRichard MacCutchan20-Jan-15 5:42 
QuestionAdding a second recent file list to the MFC ribbon bar Pin
Leif Simon Goodwin19-Jan-15 23:18
Leif Simon Goodwin19-Jan-15 23:18 
AnswerRe: Adding a second recent file list to the MFC ribbon bar Pin
Leif Simon Goodwin23-Jan-15 0:23
Leif Simon Goodwin23-Jan-15 0:23 
GeneralRe: Adding a second recent file list to the MFC ribbon bar Pin
Leif Simon Goodwin25-Jan-15 22:45
Leif Simon Goodwin25-Jan-15 22:45 
QuestionC++ class initialization Pin
Vaclav_19-Jan-15 14:32
Vaclav_19-Jan-15 14:32 
AnswerRe: C++ class initialization Pin
Jochen Arndt19-Jan-15 21:18
professionalJochen Arndt19-Jan-15 21:18 
static uint32_t usb_error = 0;   // is (global) static initialized to 0 as default anyway? 

Don't care about what the compiler does. Variables that may be read before they are written should be always initialized.

usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; // why "initialized " again here? 

The variable will probably changed by the class. When creating another instance, the value is not the initial one.

bmHubPre     = 0;            // is this necessary? see constructor parameter 

The variable is set in the constructor but the init function may be called multiple times.

Overall this is a bad example. When using global static variables with a class, you must ensure that only one instance of the class exists. To detect multiple instances some kind of check should be implemented. For your class this can be done by moving the re-initialization of usb_task_state to the destructor and checking the value in the constructor:
ASSERT(usb_task_state == USB_DETACHED_SUBSTATE_INITIALIZE);


Generally there is no need to use a class when that uses global static variables. When global variables are really necessary, they should be at least members of the class:
C++
// Header file
class USBHost
{
    // ...
protected:
    static uint32_t usb_error;
    static uint32_t usb_task_state;
}

C++
// Source file
uint32_t USBHost::usb_error = 0;
uint32_t USBHost::usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;

GeneralRe: C++ class initialization Pin
David A. Gray8-Apr-15 20:25
David A. Gray8-Apr-15 20:25 
GeneralRe: C++ class initialization Pin
Jochen Arndt8-Apr-15 20:51
professionalJochen Arndt8-Apr-15 20:51 
GeneralRe: C++ class initialization Pin
David A. Gray9-Apr-15 11:05
David A. Gray9-Apr-15 11:05 
AnswerRe: C++ class initialization Pin
CPallini20-Jan-15 1:52
mveCPallini20-Jan-15 1:52 
AnswerRe: C++ class initialization Pin
Albert Holguin20-Jan-15 4:40
professionalAlbert Holguin20-Jan-15 4:40 
GeneralRe: C++ class initialization Pin
Vaclav_20-Jan-15 6:22
Vaclav_20-Jan-15 6:22 
GeneralRe: C++ class initialization Pin
Albert Holguin20-Jan-15 6:50
professionalAlbert Holguin20-Jan-15 6:50 
GeneralRe: C++ class initialization Pin
Vaclav_20-Jan-15 16:43
Vaclav_20-Jan-15 16:43 
GeneralRe: C++ class initialization Pin
Albert Holguin21-Jan-15 3:14
professionalAlbert Holguin21-Jan-15 3:14 
GeneralRe: C++ class initialization Pin
Vaclav_21-Jan-15 16:00
Vaclav_21-Jan-15 16:00 
GeneralRe: C++ class initialization Pin
CPallini20-Jan-15 7:56
mveCPallini20-Jan-15 7:56 
GeneralRe: C++ class initialization Pin
Albert Holguin20-Jan-15 8:05
professionalAlbert Holguin20-Jan-15 8:05 
GeneralRe: C++ class initialization Pin
CPallini20-Jan-15 9:14
mveCPallini20-Jan-15 9:14 
GeneralRe: C++ class initialization Pin
Albert Holguin20-Jan-15 9:57
professionalAlbert Holguin20-Jan-15 9:57 
GeneralRe: C++ class initialization Pin
CPallini20-Jan-15 10:26
mveCPallini20-Jan-15 10:26 
General[SOLVED] Re: C++ class initialization Pin
Vaclav_22-Jan-15 8:00
Vaclav_22-Jan-15 8:00 
GeneralRe: [SOLVED] Re: C++ class initialization Pin
CPallini22-Jan-15 9:06
mveCPallini22-Jan-15 9:06 

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.