Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 419
User Avatar asked Oct 29 '11 22:10

User


People also ask

Can you call a function from a nullptr?

A class member function can be called using a NULL object pointer. Note − This is undefined behaviour and there is no guarantee about the execution of the program. The actual results depend on the compiler used. A program that demonstrates this is given as follows.

Should you use null or nullptr?

nullptr is a new keyword introduced in C++11. nullptr is meant as a replacement to NULL . nullptr provides a typesafe pointer value representing an empty (null) pointer. The general rule of thumb that I recommend is that you should start using nullptr whenever you would have used NULL in the past.

Why should we use nullptr?

The nullptr keyword can be used to test if a pointer or handle reference is null before the reference is used. Function calls among languages that use null pointer values for error checking should be interpreted correctly. You cannot initialize a handle to zero; only nullptr can be used.

Is nullptr a literal?

The keyword nullptr denotes the null pointer literal. It is an unspecified prvalue of type std::nullptr_t. There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type.


1 Answers

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.

like image 193
GManNickG Avatar answered Oct 16 '22 18:10

GManNickG