answersLogoWhite

0


Best Answer

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

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

A reference is an alias for a variable. Unlike a pointer variable, which must occupy memory in order to store the memory address of the object it points to, a reference consumes no memory other than that used by the object it references.

An array name is a typical example of a reference. It is an alias to the memory allocated to the array.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

No. Automatic and local function variables are allocated on the heap and will fall from scope when a function returns. You cannot refer to an out of scope variable. To get around this, allocate new memory to a local pointer variable on the free store and return the pointer by value (which returns a copy of the memory address of the allocation). The only problem with this is that the calling function is then responsible for assigning the returned pointer and freeing the allocation when it is no longer required.

Another option is to declare the local function variable static, thus it won't fall from scope when the function returns. However, if the function is called more than once you will then end up with multiple references to the same memory, which may be altered by the last call to the function.

The safest way to return by reference is to pass a reference to the function (the return reference needn't be the address of the reference that was passed, but must be a reference within the referred allocation). If the function is a class member, returning a reference to another member of the same class is also acceptable, so long as the class instance remains in scope for the lifetime of the returned reference.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you return by reference in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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 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 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 is the size of a blank class in C plus plus?

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