chip_munks
chip_munks

Reputation: 45

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.

Upvotes: 3

Views: 167

Answers (2)

bdonlan
bdonlan

Reputation: 231063

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.

Upvotes: 1

John Dibling
John Dibling

Reputation: 101446

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.

Upvotes: 6

Related Questions