0

Hello there. I have here a constructor of class CDriveControl and class CSPTIDriver..

class CSPTIDriver;
class CDriveControl{
   public:
      CDriveControl();
      CDriveControl(const char* pPort);
       ......
       ......
 
   protected:
      char* m_pPort;
      CSPTIDriver* m_pSPTIDriver;
      bool m_bInitFlag;
      bool m_onlycomp;
}

It was defined outside the class.

CDriveControl::CDriveControl(void)
:m_pPort(NULL)                        //Object drive
,m_pSPTIDriver(NULL)                //SPTI object
,m_bInitFlag(false)                    //Formatted flag
,m_onlycomp(false)
{
}

CDriveControl::CDriveControl(const char* pPort)
:m_pPort(NULL)                        //Object drive
,m_pSPTIDriver(NULL)                //SPTI object
,m_bInitFlag(false)                    //Formatted flag
,m_onlycomp(false)
{    
    if(pPort == NULL){
        Trace(false, "CDriveControl();pPort==NULL\n");
        return;
    }
    //Port name is kept
    m_pPort = new char[strlen(pPort)+1];
    strcpy(m_pPort, pPort);
//    ::InitializeCriticalSection(&m_cCrtSection);
    
}

My question is, what is the reason that these protected variables where placed here?

CDriveControl::CDriveControl(void)
:m_pPort(NULL)                    //<---here    
,m_pSPTIDriver(NULL)          //<---also here      
,m_bInitFlag(false)               //<---guess what?    
,m_onlycomp(false)             //<---you got it right again!
{ //<---what's up with this?
} //<---and this one too?

I searched some function tutorials but I was not able to find any explanation with this one. I'm currently studying classes and I was able to find this example code of constructor. Could anyone explain to me what is the use of doing that? I'm a having a hard time realizing what in the world is it's purpose. Thank you very much...

3
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by jaepi
0

Its just another way of initializing variables -- those could have just as easily been coded like this:

CDriveControl::CDriveControl()
{
     m_pPort = NULL;
     m_pSPTIDriver = NULL;
     m_bInitFlag = NULL;
     m_onlycomp = NULL;
}
0

Its just another way of initializing variables -- those could have just as easily been coded like this:

CDriveControl::CDriveControl()
{
     m_pPort = NULL;
     m_pSPTIDriver = NULL;
     m_bInitFlag = NULL;
     m_onlycomp = NULL;
}

Oh, I see. Is there an advantage of using the one that I've mentioned to the one that you stated? Is there any critical difference? Thank you for putting out my worries.

0

you must use the initialization list version to initialize references. Otherwise I don't think there is a difference. I prefer the version I posted whenever possible.

0
struct A
{
   std::string str ;
   explicit A( const char* cstr ) ;
   explicit A( const std::string& s ) ;
};

A::A( const char* cstr ) : str(cstr) {}
A::A( const std::string& s ) { str = s ; }

the constructor using initializer syntax is more efficient; it directly constructs the string from a const char*. the one using assignment first constructs a string using its default constructor and then assigns one string to another.
there would be no difference if the default initialization is trivial (for example an int member); initializer syntax is required if there is no default initialization available(reference or const members, user defined type with no or inaccessible default constructors).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.