answersLogoWhite

0

Returning by reference implies that the reference will not fall from scope when the function returns (because references must never be NULL). Thus you cannot return a reference to a variable that was declared local to the function.

int& foo()

{

int local;

return(( int & ) local); // Error: cannot return a reference to a local variable.

}

However, you can return a reference to a parameter that was itself passed by reference, since that reference is guaranteed to exist (it can't be NULL).

const int& GetMax( const int& byRef1, const int& byRef2 )

{

return( byRef1 > byRef2 ? byRef1 : byRef2 ); // this is fine.

}

Generally, the only time you will return by reference is when returning a class instance member or a reference to the class instance itself, since they always remain in scope after the function call.

class bar

{

public:

bar():m_num(0){}

const int& GetIntRef()const{return((const int&)m_num);} // constant reference to instance member.

int& GetIntRef(){return((int&)m_num);} // non-constant reference to instance member.

const bar& AsRef()const( return((const bar& )*this );} // constant reference to this instance.

bar& AsRef()( return((bar&)*this );} // non-constant reference to this instance.

private:

int m_num;

};

Note that use of const is not obligatory when returning references. However, when values must not be changed, it's good policy to enlist the help of the compiler wherever possible. The previous example returns both constant and non-constant references. The function that makes a call to GetIntRef() or AsRef() will determine whether the returned reference should be constant or not. By implementing both methods, we cater for both scenarios.

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What does malloc return in C and C plus plus?

Address of the allocated area, or NULL.


Fill in blanks in c plus plus?

a functon that doesn't return anything has return type


What are the different parameter passing methods used by c plus plus?

Pass by value, constant value, reference and constant reference. Pass by value is the default in C++ (pass by reference is the default in Java).


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What do the two plus stand for in C plus plus?

The ++ in C++ refers to the postfix increment operator (operator++()). It's literal meaning is "the successor to C", in reference to the C language upon which the C++ language is based.


C plus plus program using function min that take three parameters of type int and returns the minimum of the three?

int min (int a, int b, int c) {if (a


What is a call by reference?

In C++ (C Plus Plus), when you call by reference, you are directly accessing the data of the referenced object. When you pass an object to a function by reference, any and all alterations to the object made within the function carry through to the actual object.


What is calling by reference?

In C++ (C Plus Plus), when you call by reference, you are directly accessing the data of the referenced object. When you pass an object to a function by reference, any and all alterations to the object made within the function carry through to the actual object.


What use of void data type in c plus plus?

doesn't return the value.


What is a reference variable in c plus plus?

A reference variable in C++ is a formal parameter of a function call that automatically dereferences itself, as if it were a pointer, into a reference to the original value in the calling routine. You declare the reference type in the function declaration and prototype, but the compiler automatically adds the reference (&) operator on call, and the dereference (*) operator on use.


What does c plus plus use call by value or call by reference?

When we call a function in C++ by passing the values as arguments, it is called call by value. e.g #include<iostream.h> #include<conio.h> int add(int,int); int main() { int a,b,c; cout<<"Enter numbers."; cin>>a>>b; c=add(a,b); cout<<"Sum : "<<c; return 0; } int add(int a,int b) { int c; c=a+b; return c; }


What is the size of a blank class in C plus plus?

Try 'sizeof', it will return the size in bytes.