C++/c++
Expert: Zlatko - 8/27/2010
Questionwill return types of different versions of the functions in case of function overriding?
they will have same or different return types?
AnswerHello Ranjita
OVERLOADING
====================
Overloaded functions with the same parameter list must all have the same return types. It is an error to have different return types if the parameter lists are the same.
The following is an error because the function names and the parameter lists are the same. The functions differ only by return type.
int func(int x);
float func(int x);
The following is allowed. The parameter lists are different, so the return types can be different, even though the function names are the same.
int func(int x);
float func(int x, int y);
The following is also correct. The parameter lists are different, and the return types are the same.
int func(int x);
int func(float x);
OVERRIDING
=============
Overriding can have different return types, although it makes the program confusing. I think it is probably a bad programming practice.
For example, the following is allowed:
class A
{
public:
int func(int x);
};
class B : public class A
{
public:
float func(int x);
};
BUT it is not allowed if the func in class A is virtual. Virtual functions must have the same return type.
I know it is a lot to remember, but C++ can be complicated. I hope that helps you.
Best regards
Zlatko