Link to home
Start Free TrialLog in
Avatar of supportoranges
supportorangesFlag for United States of America

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.findOrangeByID(id); //no oranges yet
myOrangeRef.someFunc();//succeeds, but WHY? //No object exists why does call succeed

CODE
//caller
orange& myOrangeRef=theApp.findOrangeByID(id);


//called function
orange& CProject2App::findOrangeByID(integer id)
{
        .....
        .....
      return *pOrange;  //pOrange is NULL, no error in CALLEE
}
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

typo - Should be " or, it is not referring to any orange class data member.
(looks like I was typing while ambience was submitting)
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
    }
Avatar of supportoranges

ASKER

Thanks for contributing -- I feel like I know so much more about references now!
You are both right - I never accessed data members so it never crashed and I will redesign my construct.  Thank you.