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.
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.
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.
Address of the allocated area, or NULL.
True - A C++ constructor cannot return a value.
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; }
No. There is no default return type for functions, it must be explicitly specified in both the function declaration and in the definition. To specify no return value, return void. To return a variant type, return void* (pointer to void). Otherwise return the exact type.
A typedef is a compiler macro. A reference is a pointer, usually implemented with transparent syntax. They have no relationship between each other.
Address of the allocated area, or NULL.
a functon that doesn't return anything has return type
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 - A C++ constructor cannot return a value.
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.
int min (int a, int b, int c) {if (a
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.
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.
doesn't return the value.
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; }
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.
Try 'sizeof', it will return the size in bytes.