How check a variable is null?

This is a discussion on How check a variable is null? within the C++ Programming forums, part of the General Programming Boards category; Hoe do I check that something's null, like in: Code: class Test { private: std::string name; Test prev; Test(Test previous, ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How check a variable is null?

    Hoe do I check that something's null, like in:

    Code:
    class Test
    {
    private:
         std::string name;
         Test prev;
    
         Test(Test previous, std::string n) : prev( previous ), name( n )
         {}
    
         Test(std::string n) : name( n ) {}
    
         const char* test()
         {
              //I want to do smth similar to this type of code:
              if ( prev != null ) return ...elided...;
              ..rest elided...
         }
    }
    How do I check to see if "prev" was ever set?
    Last edited by 6tr6tr; 04-10-2008 at 03:14 PM.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    NULL == 0

    So prev would have to be a pointer for that to work.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by Raigne View Post
    NULL == 0

    So prev would have to be a pointer for that to work.
    Right I know. I want to know how I can check if that variable was ever set. How do I do that? (Because if it wasn't set and I try to call a member on it, I'll get a runtime error)

    So how do I know it's ok to call a member on it?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    [QUOTE=6tr6tr;740392]Hoe do I check that something's null, like in:

    Code:
    class Test
    {
    private:
         std::string name;
         Test prev;
    
         Test(Test previous, std::string n) : prev( previous ), name( n )
         {}
    
         Test(std::string n) : name( n ) {}
    
         const char* test()
         {
              //I want to do smth similar to this type of code:
              if ( prev != null ) return ...elided...;
              ..rest elided...
         }
    }
    Unfortunately this code example will not even compile, because you have an instance of "Test" inside "Test" itself, in other words, an infinite recursion. You have to use a pointer or reference -- in this case, a pointer, since you want null checking.

    The value of "null" is 0. So to see if a pointer is pointing "nowhere," just compare it with 0. In C we had the NULL constant -- in C++ it is preferred to just use 0.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    A class instance is never not initialized (unless it is a POD), so you don't have to worry about an instance being null.

    >> So how do I know it's ok to call a member on it?
    It should always ok to call a member function (or use a member variable) of an instance of a class. It is only a pointer that could cause a problem in your program if you use it without being initialized.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by Daved View Post
    A class instance is never not initialized (unless it is a POD), so you don't have to worry about an instance being null.

    >> So how do I know it's ok to call a member on it?
    It should always ok to call a member function (or use a member variable) of an instance of a class. It is only a pointer that could cause a problem in your program if you use it without being initialized.
    OK, thanks! That makes sense now. It's very different from java.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by Daved View Post
    A class instance is never not initialized (unless it is a POD), so you don't have to worry about an instance being null.

    >> So how do I know it's ok to call a member on it?
    It should always ok to call a member function (or use a member variable) of an instance of a class. It is only a pointer that could cause a problem in your program if you use it without being initialized.
    Hmmm, my program's still not working. I'm now passing a pointer "Test* previous" and setting it equal to a pointer in th class. But when I check:

    if ( test > 0 )

    It always returns as true even if I pass "NULL" into the constructor. What am i doing wrong?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> It's very different from java.
    Yes, very different. If you want a pointer or reference, you have to explicitly state that by using * or &. Otherwise, C++ does a lot of stuff by value, and to be good at it you need to be able to do things by value in addition to by reference.

    >> What am i doing wrong?
    I don't know, can you post the code?

    BTW, normally you use if (test != 0).

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Don't use relational operators on pointers. Use !=.

    But that's probably not the problem. Code?

    By the way, in Java, all variables of object type are pointers (Java calls them references). In C++, variables are objects unless you explicitly make them pointers or references.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by CornedBee View Post
    Don't use relational operators on pointers. Use !=.

    But that's probably not the problem. Code?

    By the way, in Java, all variables of object type are pointers (Java calls them references). In C++, variables are objects unless you explicitly make them pointers or references.
    Code:
    class Test
    {
    
    private:
          std::string name;
          Test* test;
    
    public:
          Test(std::string n) 
          {
                Test( n, NULL );
          }
    
          Test(std::string n, Test* other): name( n )
          {
                test = other;
    
                //This says it is zero when I call other constructor, so it's right
                if ( test == 0 ) printf( "null in constructor" );
          }
    
          const char* print()
          {
                //This does NOT print out, somehow it's no longer 0
                if ( test == 0 ) printf( "null in print" );
                return n;
          }
    
    
    }
    
    
    int main()
    {
          Test t( "name" );
    
          //This says it's not null
          t.print();
          return 0;
    }
    EDIT: OK, why is this?

    Code:
    If I change the first constructor to this:
    
          Test(std::string n) :name( n );
          {
                test = NULL;
          }
    
    then it works!! Why? What's the difference?
    Last edited by 6tr6tr; 04-10-2008 at 04:24 PM.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You can't call one constructor from another constructor. You can make one constructor with a default value for test:
    Code:
    Test(std::string n, Test* other = 0): name( n ), test(other)
    Otherwise you just have to initialize name and test in both constructors separately.
    Last edited by Daved; 04-10-2008 at 04:25 PM.

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by Daved View Post
    You can't call one constructor from another constructor. You can make one constructor with a default value for test:
    Code:
    Test(std::string n, Test* other = 0): name( n ), test(other)
    Otherwise you just have to initialize name and test in both constructors separately.
    Yep, thanks! I just figured that out. You can do it in java so I didn't realize there was no way to do it in C++. Too bad, it's very convenient.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by 6tr6tr View Post
    Yep, thanks! I just figured that out. You can do it in java so I didn't realize there was no way to do it in C++. Too bad, it's very convenient.
    Supposedly, the ability to chain constructors is coming in C++0x.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by 6tr6tr View Post
    Yep, thanks! I just figured that out. You can do it in java so I didn't realize there was no way to do it in C++. Too bad, it's very convenient.
    Yeah, but Java doesn't let you set default values, so C++ loses a point and so does Java.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Supposedly, the ability to chain constructors is coming in C++0x.
    It's in the working paper.
    Code:
    foo::foo() : foo(x, y, z) {}
    The bad part for Java programmers isn't that C++ doesn't let you chain constructors. It's that the syntax used for this in Java - with this replaced by the class name - is actually valid. It just doesn't really do anything.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  3. Tweakable Radar...
    By DoraTehExploda in forum Game Programming
    Replies: 8
    Last Post: 06-07-2005, 10:49 AM
  4. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 04:01 AM
  5. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21