Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to check if a reference is NULL

how to check if a reference is NULL

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ng kok chuan
    wrote on last edited by
    #1

    hi, i have a class that is defined below: class DVertex { public: DVertex(MazeNode*) { this->node = n; this->parent = NULL; this->cumulativeCost = DBL_MAX; } // member functions void update(MazeNode*, double); // update the parent and cost. double getCCost() const { return cumulativeCost; } // required to relax the node. MazeNode& getNode() const { return *node; } // required to identify the node. MazeNode& getParent() const { return *parent; } // required for reverse lookup. private: MazeNode *node; MazeNode *parent; double cumulativeCost; }; now the problem is when i tried to check the value of parent, i.e. void main() { DVertex v; if (v.getParent()==NULL) ; // do something } this will not work.. however if i change the class to return me MazeNode*, i.e. MazeNode* getParent() const { return parent; } then i can do that the check. anyone know how to check if a reference is null? and by the way, can tell me how to change the inline constructor to something shorter? i remember seeing something like Constructor( par1):var1=par1; but can't remember the exact syntax. thanks!!

    R 1 Reply Last reply
    0
    • N ng kok chuan

      hi, i have a class that is defined below: class DVertex { public: DVertex(MazeNode*) { this->node = n; this->parent = NULL; this->cumulativeCost = DBL_MAX; } // member functions void update(MazeNode*, double); // update the parent and cost. double getCCost() const { return cumulativeCost; } // required to relax the node. MazeNode& getNode() const { return *node; } // required to identify the node. MazeNode& getParent() const { return *parent; } // required for reverse lookup. private: MazeNode *node; MazeNode *parent; double cumulativeCost; }; now the problem is when i tried to check the value of parent, i.e. void main() { DVertex v; if (v.getParent()==NULL) ; // do something } this will not work.. however if i change the class to return me MazeNode*, i.e. MazeNode* getParent() const { return parent; } then i can do that the check. anyone know how to check if a reference is null? and by the way, can tell me how to change the inline constructor to something shorter? i remember seeing something like Constructor( par1):var1=par1; but can't remember the exact syntax. thanks!!

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      A reference by definition can never be null. If your method could return NULL, then you'll need to return a pointer, not a reference. ng kok chuan wrote: can tell me how to change the inline constructor to something shorter? What's wrong with what's there? Is concise, easy to understand, easy to maintain... Why make it harder?

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      N 1 Reply Last reply
      0
      • R Ryan Binns

        A reference by definition can never be null. If your method could return NULL, then you'll need to return a pointer, not a reference. ng kok chuan wrote: can tell me how to change the inline constructor to something shorter? What's wrong with what's there? Is concise, easy to understand, easy to maintain... Why make it harder?

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        N Offline
        N Offline
        ng kok chuan
        wrote on last edited by
        #3

        Ryan Binns wrote: What's wrong with what's there? Is concise, easy to understand, easy to maintain... Why make it harder? it's just for knowledge's sake... :) and thanks for clarifying the reference. so i'm not wrong when i changed everything to return pointers. now my code looks incomprehensible.. with both pointers and references... and i'm not going to go and standardize my code :p it's going to be fine as it stands.

        S 1 Reply Last reply
        0
        • N ng kok chuan

          Ryan Binns wrote: What's wrong with what's there? Is concise, easy to understand, easy to maintain... Why make it harder? it's just for knowledge's sake... :) and thanks for clarifying the reference. so i'm not wrong when i changed everything to return pointers. now my code looks incomprehensible.. with both pointers and references... and i'm not going to go and standardize my code :p it's going to be fine as it stands.

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          To quench your thirst for knowledge :)

          class Foo
          {
          int x,y;
          public Foo(int _x, int _y) : x(_x), y(_y)
          {}
          }

          And there are a few reasons to prefer this syntax to manually assigning values to members. For one, if you are initializing const or reference members, you'd have to use the above syntax. Two, using the above syntax is more efficient. If you use manual member assignment, there will be two calls (constructor and operator=) for every non-POD member, if you use the above syntax, there will be only one. For eg,

          class Foo
          {
          string x;
          public Foo(string y) : x(y) {}
          }

          There will be only constructor call to string with the above syntax, as the compiler calls string(const string &) for initializing x. If you do

          class Foo
          {
          string x;
          public Foo(string y)
          {
          x = y;
          }
          }

          there will be two, string() and operator=() on string. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups