12

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?

1 Answer 1

18

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure that's exactly true from reading here. 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: The premise of my answer is that NULL was being used correctly, in the context of pointers.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.