11

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?

Exa
  • 4,020
  • 7
  • 43
  • 60
User
  • 62,498
  • 72
  • 186
  • 247

1 Answers1

17

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.

Community
  • 1
  • 1
GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • 3
    I'm not sure that's exactly true from reading [here](http://stackoverflow.com/questions/1282295/what-exactly-is-nullptr-in-c0x). If a NULL was placed somewhere it should of been 0 before than just replacing all of NULL with nullptr would break code. I see tutorials everywhere that set handles and kinds of variable types to NULL. Types that shouldn't be assigned as nullptr. – Joe McGrath Oct 30 '11 at 00:17
  • 2
    @Joe: The premise of my answer is that NULL was being used correctly, in the context of pointers. – GManNickG Oct 30 '11 at 03:29
  • 7
    @User: this might be confusing, but `DWORD_PTR` is not a pointer type, but an integer. This means that, as @JoeMcGrath pointed out, you will need to use `0` in that context, not `nullptr`. – André Caron Oct 30 '11 at 23:22