((serverDlg*)m_pDlg)->OnReceive(port);

I built a server using MFC, and had to get the foundation going by using examples online. The OnReceieve(port), is a function that I created in my CDialog class.

((serverDlg*)m_pDlg) is the part I don't get. This line of code is in a function in my socket class, and I am able to call a function that is declared in my CDialog class(OnReceieve(port)). serverDlg is the name of my CDialog class and m_pDlg is a pointer to a CDialog object if that helps. Thanks.

Recommended Answers

All 7 Replies

CDialog is the class name of a generic dialog class. serverDlg is a class that was derived from CDialog. So if m_pDlg was declared like this: CDialog* m_pDlg; then later assigned to a pointer of type serverDlg then the line you are confused about is typecasting m_pDlg from CDialog to serverDlg so that it can call the OnReceive() method. The typecasting would not have been necessary had m_pDlg been declared as type serverDlg instead of CDialog.

Ok, I understand for the most part, but I dont get how what you just explained translated into ((serverDlg*)m_pDlg)->OnReceive(port);

I don't understand the use of the dereference operator. And I thought type casting was taking a data type such as int var1, and going char var2 = (char)var1.

Edit: You changed your profile picture as i refreshed the page =D

The deference operator is used because m_pDlg is a pointer and it needs to be dereferenced to access the object at this location.

If the OnReceive method belongs to CDialog and m_pDlg is a CDialog, then you don't need to cast to ServerDlg.

The typecast is needed to convert m_pDlg from CDialog to serverDlg. And as ^^^ explained.

From looking at the line of code in question:

((serverDlg*)m_pDlg)->OnReceive(port);

If typecasting is required, shouldn't one of the C++ cast operators like dynamic_cast be used to safely typecast the pointer, instead of doing a potentially unsafe C-style cast?
e.g.

// attempt to cast m_pDlg using dynamic_cast
serverDlg *pSrvDlg = dynamic_cast<serverDlg*>(m_pDlg);

// if we've got a valid pointer, call the OnRecieve function
if(pSrvDlg)
    pSrvDlg->OnRecieve(port);
else
    // Some code here to handle the eventuality that m_pDlg
    // could not successfully be cast from CDialog* to serverDlg*

If there's any possibility that m_pDlg could point to a different CDialog based class, using dynamic_cast would be critical to avoid a runtime error/segfault in cases where m_pDlg points to a different type of CDialog derived object.

However, if you 100% know that the object pointed to by m_pDlg will only ever be a serverDlg object, then perhaps you should change the type of m_pDlg at declaration to be a serverDlg pointer rather than a CDialog pointer, which would then completely negate the need to typecast.

That's my two pence on the matter!
Cheers for now,
Jas.

You are probably right. But MFC was originally written before RTTI and has its own run time type information. This is discussed in more detail at the end of this link.

Thanks for clearing everything up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.