-
March 26th, 2007, 06:12 PM
#1
Overload vs Override
Very Tricky and confusing...
Can someone clearly distinguish between the two ?
Thanks
-
March 26th, 2007, 07:35 PM
#2
Re: Overload vs Override
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.
-
March 26th, 2007, 07:35 PM
#3
Re: Overload vs Override
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
-
March 26th, 2007, 08:00 PM
#4
Re: Overload vs Override
Thnaks to both of you guys
cheers
-
March 26th, 2007, 08:31 PM
#5
Re: Overload vs Override
Hi all.
Originally Posted by JVene
It's actually simple....these are overloads....
Code:
class A
{
public:
void func( int );
};
class B : public A
{
public:
void func( int );
};
Based on the code above, it's also worth to make two observations:
- 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;
}
-
March 27th, 2007, 03:34 AM
#6
Re: Overload vs Override
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 27th, 2007, 03:48 AM
#7
Re: Overload vs Override
Originally Posted by JVene
It's actually simple....these are overloads....
You can't differentiate overloads if the only difference is the return type.
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.
Thanx
-
March 27th, 2007, 01:23 PM
#8
Re: Overload vs Override
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.
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.
Indeed - they can be confusing unless they are virtual functions.
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.
-
March 28th, 2007, 03:43 AM
#9
Re: Overload vs Override
Originally Posted by JVene
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.
This is also clear from the language standard.
"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();
};
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
March 28th, 2007, 07:47 AM
#10
Re: Overload vs Override
Originally Posted by SuperKoko
Function/variable hiding is a simple consequence of rules concerning class scopes.
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...
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|