supportoranges
asked on
Reference to no object does not produce any run time error (MSVS2010 C++)
//my program wants to get a reference to an orange.
orange& myOrangeRef=theApp.findOra ngeByID(id ); //no oranges yet
myOrangeRef.someFunc();//s ucceeds, but WHY? //No object exists why does call succeed
CODE
//caller
orange& myOrangeRef=theApp.findOra ngeByID(id );
//called function
orange& CProject2App::findOrangeBy ID(integer id)
{
.....
.....
return *pOrange; //pOrange is NULL, no error in CALLEE
}
orange& myOrangeRef=theApp.findOra
myOrangeRef.someFunc();//s
CODE
//caller
orange& myOrangeRef=theApp.findOra
//called function
orange& CProject2App::findOrangeBy
{
.....
.....
return *pOrange; //pOrange is NULL, no error in CALLEE
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Although this is the first time I've seen a reference having a NULL value, (so I'm guessing this isn't standard or good practice), if you actually did have a need to pass this exceptional NULL case using your constructs, then the caller needs to test for this condition, as follows:
if( &myOrangeRef == NULL ) {
// Handle the exceptional NULL case condition here
}
else {
// normal processing here
}
if( &myOrangeRef == NULL ) {
// Handle the exceptional NULL case condition here
}
else {
// normal processing here
}
ASKER
Thanks for contributing -- I feel like I know so much more about references now!
ASKER
You are both right - I never accessed data members so it never crashed and I will redesign my construct. Thank you.
(looks like I was typing while ambience was submitting)