Search code examples
c++winapimfcnullnullptr

Using nullptr in API function calls?


Using C++ with Visual Studio 2010. I'm in the process of converting my NULL's to nullptr's. With my code this is fine. However if I make a call to WINAPI such as:

__checkReturn WINOLEAPI OleInitialize(IN LPVOID pvReserved);

normally I would have called this like:

::OleInitialize(NULL);

Can I safely use nullptr where I would have used NULL in a call such as this?

That is, can I do this:

::OleInitialize(nullptr);

Also same with MFC api:

CFileDialog fileDlg(TRUE, ".txt", NULL, 0, strFilter);

Can I replace

CFileDialog fileDlg(TRUE, ".txt", nullptr, 0, strFilter);

I'm guessing I can but I just want to make sure there are no gotchas.

UPDATE

So I went through and replaces all my NULL's with nullptr and it seems to work most everywhere however I am getting the below error on the following line:

propertyItem = new CMFCPropertyGridProperty(_T("SomeName"),
"SomeValue", "SomeDescription", nullptr, nullptr, nullptr, nullptr);

8>c:\something\something.cpp(118): error C2664: 'CMFCPropertyGridProperty::CMFCPropertyGridProperty(const CString &,const COleVariant &,LPCTSTR,DWORD_PTR,LPCTSTR,LPCTSTR,LPCTSTR)' : cannot convert parameter 4 from 'nullptr' to 'DWORD_PTR' 8> A native nullptr can only be converted to bool or, using reinterpret_cast, to an integral type

(Note CMFCPropertyGridProperty is a Microsoft MFC class) So what does that mean?


Solution

  • Yes, you can safely use nullptr anywhere you use NULL.

    NULL expanded to an integer constant expression with the value zero, which could then be converted to a null pointer value of any type. nullptr is "pointer literal" that does the exact same thing: it converts to a null pointer value of any type.

    More information here.