Returning a refrence from a function
-
Hi there , I have a class lets say CMyClass, it contains a member variable of type CArray of lets say int. Now I have a public function of this Class GetValue(int nIndex) now this function returns the Reference of int which is actually a reference to a particular index of my CArray type member variable. Now here is the problem .. My GetValue function performs some checks before returning the Refrence . now in case the checks fails I dont want to return the reference .. in that case WHAT DO I RETUNR FROM MY CODE. does any body understand what I am trying to ask ? Abhishek Narula "Learn to appreciate others ... World would appreciate you"
-
Hi there , I have a class lets say CMyClass, it contains a member variable of type CArray of lets say int. Now I have a public function of this Class GetValue(int nIndex) now this function returns the Reference of int which is actually a reference to a particular index of my CArray type member variable. Now here is the problem .. My GetValue function performs some checks before returning the Refrence . now in case the checks fails I dont want to return the reference .. in that case WHAT DO I RETUNR FROM MY CODE. does any body understand what I am trying to ask ? Abhishek Narula "Learn to appreciate others ... World would appreciate you"
Throw an exception. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Hi there , I have a class lets say CMyClass, it contains a member variable of type CArray of lets say int. Now I have a public function of this Class GetValue(int nIndex) now this function returns the Reference of int which is actually a reference to a particular index of my CArray type member variable. Now here is the problem .. My GetValue function performs some checks before returning the Refrence . now in case the checks fails I dont want to return the reference .. in that case WHAT DO I RETUNR FROM MY CODE. does any body understand what I am trying to ask ? Abhishek Narula "Learn to appreciate others ... World would appreciate you"
-
Throw an exception. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Hmm that seems a better idea .. please throw some light on thoring exceptions also .. Abhishek Narula "Learn to appreciate others ... World would appreciate you"
-
Throw an exception. There's no reference equivalent of a NULL pointer, so you must either return a valid reference or throw.
Wonderfull Idea !! thanks .. please tell me more about throwing exceptions also .. Abhishek Narula "Learn to appreciate others ... World would appreciate you"
-
Hmm that seems a better idea .. please throw some light on thoring exceptions also .. Abhishek Narula "Learn to appreciate others ... World would appreciate you"
Essentially, you just throw with
throw
, which interrupts the normal flow of execution and throws an object that can be catched by some point up the call stack withcatch
. Just read your docs forthrow
andcatch
, and/or search some tutorial on the net, sure there're many out there. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
Hmm that seems a better idea .. please throw some light on thoring exceptions also .. Abhishek Narula "Learn to appreciate others ... World would appreciate you"
It goes like this: 1. Create a class CMyException. Here is an example:
class CMyException
{
CString strError;
public:
CMyException (CString& a) : strError (a) {}
void HandleError ()
{
AfxMessageBox (strError);
}
}2. Do this in that function:
if (index_is_out_of_bounds)
throw CMyException ("Index out of bounds"); // throw the exception3. Catch the exception like this:
try
{
// call the function that may throw the exception
}
catch (CMyException& e)
{
// caught an exception and now handle it
e.HandleError ();
return;
}You can also check MSDN for details or download a good C++ book. "Thinking in C++" from www.bruceeckel.com Best regards, Alexandru Savescu