Thread: Private static member set to NULL directly?

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by dennis.cpp View Post
    (Thanks btw for that hint to nullptr - since my textbook uses NULL, I will research about that on this very website.)
    You shouldn't use NULL in C++. nullptr is great, but it's new to C++11. With compilers that don't have it, use 0. This can make some more obscure bugs more apparent.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What's wrong with NULL?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    nullptr offers some advantages over NULL (eg more type safety). That (contrary to what some zealots claim) is not saying NULL is evil. The choice is one of trade-offs.

    NULL is technically a value of zero (or, more pedantically, a value of zero when converted to a pointer is equal to NULL) and can often be treated as an integral value. This causes some problems in C++ with template overloading, because NULL isn't necessarily a pointer. The problems have been exacerbated a bit in C++ because some popular idioms encouraged using a literal value of zero in place of NULL - which basically nudged library vendors to define NULL simply as zero. The main problems come down to type-safety, and nullptr addresses that.

    So NULL was problematic in a few edge cases (which were rare in practice), some practices were encouraged (and became more mainstream) that caused it to be problematic more often, and a new keyword is introduced to fix the problem. I personally characterise all that as a self-licking icecream. The result, however, is that you are better off using nullptr than NULL if you are solely targeting compilers that support it. But you will have to keep using NULL (in one form or another) if you care about compatibility of your code (for example, ability to build with compilers that predate 2009 or so).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Actually, I was referring to NULL vs 0 (sorry if that wasn't clear).
    I am familiar with the advantages of nullptr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    > which basically nudged library vendors to define NULL simply as zero.
    How else could NULL be defined ?

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The original reason for NULL (in C) is that there was no guarantee that a pointer with value zero would not correspond to a valid object (yes, there were examples of hardware where a pointer with all bits set (0xFFFF with 16 bit pointers) was the NULL pointer). The line was blurred later when it was decreed that a value of zero, when converted to a pointer, would be equal to the NULL pointer.

    The main remaining advantage of NULL, with modern C/C++ compilers and libraries, is probably about programmer understanding. There is a slight benefit in understanding by a programmer that
    Code:
        pvariable = NULL;
    initialises a pointer rather than an integer, or that
    Code:
        some_function(NULL);
    is passing a pointer rather than a value of zero. Some people view such benefits as significant, others do not.
    Last edited by grumpy; 12-02-2011 at 05:19 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #22
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    Actually, I was referring to NULL vs 0 (sorry if that wasn't clear).
    I am familiar with the advantages of nullptr.
    NULL is defined as the literal 0 (usually). In function overloading, 0 matches int better than any pointer. But when a programmer sees NULL they think pointer, and expect the pointer version of an overloaded function to be called. When a literal 0 is written, the error is easier to spot. Here's code illustrating this:
    Code:
    #include <cstdlib>
    
    int foo(int var){
      return 1;//error
    };
    int foo(void *var){
      return 0;//expected behavior
    }
    
    int main(){
      return foo(NULL); //returns 1
    }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #23
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by manasij7479 View Post
    > which basically nudged library vendors to define NULL simply as zero.
    How else could NULL be defined ?
    In C it's defined as ((void*)0).

    It could also be defined as a special type by the compiler, similar to null_ptr, except giving a warning instead of an error when converted implicitly to int.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Member is private..??
    By Programmer_P in forum C++ Programming
    Replies: 35
    Last Post: 06-05-2010, 12:59 AM
  2. Replies: 3
    Last Post: 04-10-2009, 02:20 AM
  3. private member wont add.
    By System_159 in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2006, 05:25 PM
  4. no change for private member ???
    By black in forum C++ Programming
    Replies: 7
    Last Post: 05-28-2004, 05:25 AM
  5. private data member with public set member function
    By Mario in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2002, 10:53 AM