how to check if a reference is NULL
-
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!!
-
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!!
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"
-
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"
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.
-
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.
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