简体   繁体   中英

initialisation list in c++

I barely know c++.Not an expert.

I am looking through an already existing code. I could not able to understand this following code.

typedef enum
{
    eEvent_MsgOk,            
    eEvent_InvalidMsgId,    
    eEvent_Failure,          
} eEventType;

class Rs232Event
{
public:
    Rs232Msg*     m_pMsg;    
    eEventType     m_eEvent;   

}
Rs232Event::Rs232Event(eEventType eEvent,Rs232Msg* pMsg)
 :  m_pMsg(pMsg), m_eEvent(eEvent)
{
    // not implemented on purpose
}

Here using the initialisation list they are intialising the values.

But the Rs232Msg class doesnt have a single parameterised constructor.

But its having a constructor which accepts 4 parameters.

I could not identify how its getting invoked.But the code runs without any error.

m_pMsg isn't an Rs232Msg class. Rather, its a pointer to an Rs232Msg class. All that is being copied is a pointer to an already-existing instance of that class, so the constructor isn't being invoked here.

m_pMsg and pMsg are pointers to Rs232Msg , so the Rs232Msg constructor isn't being invoked; you're just storing a pointer to a preexisting instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM