Click here to Skip to main content
15,881,882 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
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 
Like others mentioned this isn't the cleanest example, but I'll also take a stab at explaining some of these things...
C++
static uint32_t usb_error = 0;   // is (global) static initialized to 0 as default anyway? 
static uint32_t usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; // this state is not defined as 0 - this is OK  

Initializing a global (or a static) outside of the user function or class is always good practice. The compiler may or may not initialize for you, but why risk it.
C++
USBHost::USBHost() : bmHubPre(0) // may be a default USB hub 0 , not sure 
{
     usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; // why "initialized " again here? 
 
     init();
}

This is initializing a global that is in an undefined state back to the default. Remember that if there was an instance of USBHost that existed, he might have left the state of the variable in some unknown state. Like someone else mentioned however, this initialization does imply you should only ever have one instance of this class... otherwise you'll have a conflict with the state of this variable. With that said though, it is resetting itself, so you should be able to delete the object when not in use and make a new one when you do need it.
C#
void USBHost::init()
{
     devConfigIndex     = 0;            // USB (device ?) default index
     bmHubPre     = 0;            // is this necessary? see constructor parameter
}

init() methods are usually made when there may be a need to initialize variables outside of the constructor, or alternatively, when there are multiple constructors that all need to initialize the same variables. For example, there may be some code that gets called when the USB device gets to a certain state that requires re-initialization.... rather than destruct the object and start all over, the init() may be called to re-initialize.
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 
GeneralRe: [REOPEN} C++ class initialization and scheme Pin
Vaclav_23-Jan-15 6:49
Vaclav_23-Jan-15 6:49 
GeneralRe: [REOPEN} C++ class initialization and scheme Pin
Mike Nordell2-Feb-15 10:14
Mike Nordell2-Feb-15 10:14 
GeneralRe: [REOPEN} C++ class initialization and scheme Pin
Vaclav_2-Feb-15 10:57
Vaclav_2-Feb-15 10:57 
GeneralRe: [REOPEN} C++ class initialization and scheme Pin
Mike Nordell2-Feb-15 12:46
Mike Nordell2-Feb-15 12:46 
Questionbinary data to image VC++ Pin
Mukund Pradeep16-Jan-15 18:40
Mukund Pradeep16-Jan-15 18:40 

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.