Very Tricky and confusing...
Can someone clearly distinguish between the two ?
Thanks
Printable View
Very Tricky and confusing...
Can someone clearly distinguish between the two ?
Thanks
It's actually simple....these are overloads....
void func( int );
void func( double );
void fund( unsigned int );
They are the same function name, but are 'different' functions based on the parameters accepted.
You can't differentiate overloads if the only difference is the return type.
Key point - you can 'select' among multiple functions of the same name based on the type you pass as a parameter, they need not be member functions, but certainly can be member functions.
Override....
Code:
class A
{
public:
void func( int );
};
class B : public A
{
public:
void func( int );
};
The func in class B overrides the func of class A.
If you have a instantiation of B, and call func, the one in B will be executed, not the one in A.
If, on the other hand, you have a B, but you refer to it using a pointer to type A, the func of A would be executed.
If you declared the functions to be virtual IN A - then if you called func using a pointer to type A, but the object was actually created as type B, THEN the func in B would be called - by virtual routing.
You overload by adding multiple method definitions (same name, but different argument lists). You override by changing the definition of a base class' method (polymorphism).
- petter
Thnaks to both of you guys
cheers
Hi all.
Based on the code above, it's also worth to make two observations:Quote:
Originally Posted by JVene
- Usually, if you want to provide dynamic polymorphism, you should declare your function with the "virtual" modifier.
- Sometimes, you want to overload a member function in a class hierarchy. In this case, because classes are namespaces in C++, you need to declare that you're "using" a base class function. For example, in the code below, if you don't tell that you're using the function from class A, the code will not compile (at least if you're using a standardized compiler).
Code:class A
{
public:
void func(int);
};
class B : public A
{
public:
using A::func;
void func(int, int);
};
int main(int argc, char* argv)
{
B b;
b.func(1);
return 0;
}
We had this discussion on here some while back...
We eventually concluded that the standard's use of "override" specifically refers to virtual functions. JVene's example is not an override - Scott Meyers refers to it as a redefinition. In the absence of a specific term in the standard, we felt that "redefinition" was as good a word as any.
One point to note - overrides are a Good Thing, redefinitions are a Bad Thing.
What's the meaning of the above line.As it is already known to all of us that Function Overloading Doesn't Depend on the return type.So may be you can get a error too.Quote:
Originally Posted by JVene
Thanx
I've not read Scott Meyers. There were called overrides in the general text available back in the late 80's and early 90's, but I've not bothered to check what the lingo has evolved to these days.Quote:
We eventually concluded that the standard's use of "override" specifically refers to virtual functions. JVene's example is not an override - Scott Meyers refers to it as a redefinition.
Indeed - they can be confusing unless they are virtual functions.
Quote:
Of my quote :)
You can't differentiate overloads if the only difference is the return type.
If the function signature of two proposed overloads are identical except for the return type, the compiler generates and error.
void func( int );
bool func( int );
The second one will generate an error.
This is also clear from the language standard.Quote:
Originally Posted by JVene
"hiding" and overriding are two very different things. It applies to all members: static or non-static, data or function.
Function/variable hiding is a simple consequence of rules concerning class scopes.
Similarly, there is a third "thing" that I wouldn't call overriding but may look like overriding for an inexperienced programmer who doesn't master the language:
Code:class X {
public:
void func();
};
class Y:public X {
#define func other_func /* This is neither overriding, nor scope hiding, it's token replacement */
void other_func();
};
I would have said that it's a simple consquence of the rules concerning name lookup, but they amount to pretty much the same thing...Quote:
Originally Posted by SuperKoko