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.
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; }
A typedef is a compiler macro. A reference is a pointer, usually implemented with transparent syntax. They have no relationship between each other.
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.
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.
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.
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; }
Try 'sizeof', it will return the size in bytes.