nullptr

Are these null pointers, or are they pointers to address 0?

早过忘川 提交于 2019-11-27 02:37:04
问题 If I write int zero = 0; void *p1 = (void *)0; void *p2 = (void *)(int)0; void *p3 = (void *)(0 /*no-op, but does it affect the next zero?*/, 0); void *p4 = (void *)zero; // For reference, this is a pointer to address zero void *p5 = 0; // For reference, this is a null pointer void *p6 = NULL; // For reference, this is a null pointer void *p7 = nullptr; // For reference, this is a null pointer (C++11) static const int static_zero_1 = 0; // Is this a literal zero when used? static const int

c++ access static members using null pointer

亡梦爱人 提交于 2019-11-26 20:42:54
Recently tried the following program and it compiles, runs fine and produces expected output instead of any runtime error. #include <iostream> class demo { public: static void fun() { std::cout<<"fun() is called\n"; } static int a; }; int demo::a=9; int main() { demo* d=nullptr; d->fun(); std::cout<<d->a; return 0; } If an uninitialized pointer is used to access class and/or struct members behaviour is undefined, but why it is allowed to access static members using null pointers also. Is there any harm in my program? TL;DR : Your example is well-defined. Merely dereferencing a null pointer is

Can nullptr be emulated in gcc?

扶醉桌前 提交于 2019-11-26 15:31:17
问题 I saw that nullptr was implemented in Visual Studio 2010. I like the concept and want to start using it as soon as possible; however GCC does not support it yet. My code needs to run on both (but doesn't have to compile with other compilers). Is there a way to "emulate" it? Something like: #define nullptr NULL (That obviously wouldn't work well at all, it's just to show what I mean.) 回答1: The Official proposal has a workaround - const // this is a const object... class { public: template

NULL vs nullptr (Why was it replaced?) [duplicate]

浪尽此生 提交于 2019-11-26 14:04:42
This question already has an answer here: What exactly is nullptr? 12 answers I know that in C++ 0x or NULL was replaced by nullptr in pointer-based applications. I'm just curious of the exact reason why they made this replacement? In what scenario is using nullptr over NULL beneficial when dealing with pointers? nullptr is always a pointer type. 0 (aka. C's NULL bridged over into C++) could cause ambiguity in overloaded function resolution, among other things: f(int); f(foo *); Shafik Yaghmour You can find a good explanation of why it was replaced by reading A name for the null pointer:

What are the advantages of using nullptr?

不羁的心 提交于 2019-11-26 11:31:21
This piece of code conceptually does the same thing for the three pointers (safe pointer initialization): int* p1 = nullptr; int* p2 = NULL; int* p3 = 0; And so, what are the advantages of assigning pointers nullptr over assigning them the values NULL or 0 ? In that code, there doesn't seem to be an advantage. But consider the following overloaded functions: void f(char const *ptr); void f(int v); f(NULL); //which function will be called? Which function will be called? Of course, the intention here is to call f(char const *) , but in reality f(int) will be called! That is a big problem 1 , isn

c++ access static members using null pointer

╄→尐↘猪︶ㄣ 提交于 2019-11-26 08:56:57
问题 Recently tried the following program and it compiles, runs fine and produces expected output instead of any runtime error. #include <iostream> class demo { public: static void fun() { std::cout<<\"fun() is called\\n\"; } static int a; }; int demo::a=9; int main() { demo* d=nullptr; d->fun(); std::cout<<d->a; return 0; } If an uninitialized pointer is used to access class and/or struct members behaviour is undefined, but why it is allowed to access static members using null pointers also. Is

NULL vs nullptr (Why was it replaced?) [duplicate]

守給你的承諾、 提交于 2019-11-26 03:35:02
问题 This question already has answers here : What exactly is nullptr? (12 answers) Closed 5 years ago . I know that in C++ 0x or NULL was replaced by nullptr in pointer-based applications. I\'m just curious of the exact reason why they made this replacement? In what scenario is using nullptr over NULL beneficial when dealing with pointers? 回答1: nullptr is always a pointer type. 0 (aka. C's NULL bridged over into C++) could cause ambiguity in overloaded function resolution, among other things: f

What exactly is nullptr?

你。 提交于 2019-11-25 21:58:51
问题 We now have C++11 with many new features. An interesting and confusing one (at least for me) is the new nullptr . Well, no need anymore for the nasty macro NULL . int* x = nullptr; myclass* obj = nullptr; Still, I am not getting how nullptr works. For example, Wikipedia article says: C++11 corrects this by introducing a new keyword to serve as a distinguished null pointer constant: nullptr. It is of type nullptr_t , which is implicitly convertible and comparable to any pointer type or pointer