By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

Overloading/Overriding ?

P: n/a
Hello everybody,

I'm new to C++ (I have some C background). I've read up on this topic a
few times but it just dosen't seem to be sinking in.

1. Whats the difference between overloading and overriding?
2. When is one preferable to use as opposed to the other?
3. How are virtual functions related to this topic
(overloading/overriding) - a real world example of using virtual
functions would be very much appreciated.

Thank you

Jul 23 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
> 1. Whats the difference between overloading and overriding?

overloading is the definition of several functions with the same name
but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);

overriding is writing a different body (in a derived class) for a
function defined in a base class.

class base
{
public:
int Foo()
{
return 1;
}
};

class derived: public base
{
public:
int Foo()
{
return 2;
}
};
2. When is one preferable to use as opposed to the other? They are not exclusive but complimentary. It is possible to have
several overloaded functions in a base class that are overridden in a
derived class. Also overriding only works when a class is derived from
another class where overloading can be used inside a class (or for non
class function definitions).
3. How are virtual functions related to this topic

I'd suggest to read the FAQ at
http://www.parashift.com/c++-faq-lite/index.html

Jul 23 '05 #2

P: n/a
ve*********@hotmail.com wrote:
1. Whats the difference between overloading and overriding?


overloading is the definition of several functions with the same name
but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);


Just to add,
Overloading can be done without usage of classes. However if you are
using a class then these functions have to be in the same class.

Also, the return type is not relevant. Soo, as said above only the
function name should be same while paramter type/number must be
differnt.

Jul 23 '05 #3

P: n/a
ve*********@hotmail.com wrote:
1. Whats the difference between overloading and overriding?

overloading is the definition of several functions with the same name
but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);


Overloading _only_ applies to names of functions in the *same scope*.
overriding is writing a different body (in a derived class) for a
function defined in a base class.
.... and *only* if the function in the base class is declared _virtual_
and *only* if the function in the derived class has the same signature.

class base
{
public:
int Foo()
{
return 1;
}
};

class derived: public base
{
public:
int Foo()
{
return 2;
}
};
Here you have an example of name hiding since 'base::Foo' isn't virtual.
2. When is one preferable to use as opposed to the other?


They are not exclusive but complimentary. It is possible to have
several overloaded functions in a base class that are overridden in a
derived class.


This is incorrect. All functions in the derived class _hide_ the base
class functions with the same name, except in the case of virtual ones,
which override the base class functions with the _same_signature_.

Also overriding only works when a class is derived from
another class where overloading can be used inside a class (or for non
class function definitions).


Overloading only relates to the functions in the same _scope_, i.e. in
the same class or outside of any class.
3. How are virtual functions related to this topic


I'd suggest to read the FAQ at
http://www.parashift.com/c++-faq-lite/index.html


It's always a GOOD IDEA(tm).

V
Jul 23 '05 #4

P: n/a


Victor Bazarov wrote:
ve*********@hotmail.com wrote:
1. Whats the difference between overloading and overriding?

overloading is the definition of several functions with the same name
but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);


Overloading _only_ applies to names of functions in the *same scope*.
overriding is writing a different body (in a derived class) for a
function defined in a base class.


... and *only* if the function in the base class is declared _virtual_
and *only* if the function in the derived class has the same signature.

class base
{
public:
int Foo()
{
return 1;
}
};

class derived: public base
{
public:
int Foo()
{
return 2;
}
};


Here you have an example of name hiding since 'base::Foo' isn't virtual.
2. When is one preferable to use as opposed to the other?


They are not exclusive but complimentary. It is possible to have
several overloaded functions in a base class that are overridden in a
derived class.


This is incorrect. All functions in the derived class _hide_ the base
class functions with the same name, except in the case of virtual ones,
which override the base class functions with the _same_signature_.


But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that the
return type that it does declare is a virtual subclass of the return
type of the function declared in the base. [assuming compilation with a
C++ compiler that supports "covariant return types"]

Greg

Jul 23 '05 #5

P: n/a
Greg wrote:
But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that the
return type that it does declare is a virtual subclass of the return
type of the function declared in the base. [assuming compilation with a
C++ compiler that supports "covariant return types"]

Greg


Care to provide ISO/IEC 14882:2003 chapter and verse on that? Not that I
doubt what you assert, I would simply like to see how it's actually stated
in the Standard.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #6

P: n/a
Greg wrote:
Victor Bazarov wrote:
ve*********@hotmail.com wrote:
1. Whats the difference between overloading and overriding?
overloading is the definition of several functions with the same
name but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);


Overloading _only_ applies to names of functions in the *same scope*.
overriding is writing a different body (in a derived class) for a
function defined in a base class.


... and *only* if the function in the base class is declared
_virtual_ and *only* if the function in the derived class has the
same signature.

class base
{
public:
int Foo()
{
return 1;
}
};

class derived: public base
{
public:
int Foo()
{
return 2;
}
};


Here you have an example of name hiding since 'base::Foo' isn't
virtual.
2. When is one preferable to use as opposed to the other?

They are not exclusive but complimentary. It is possible to have
several overloaded functions in a base class that are overridden in
a derived class.


This is incorrect. All functions in the derived class _hide_ the
base class functions with the same name, except in the case of
virtual ones, which override the base class functions with the
_same_signature_.


But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that
the return type that it does declare is a virtual subclass of the
return type of the function declared in the base. [assuming
compilation with a C++ compiler that supports "covariant return
types"]


Yes. Covariant return types are part of the definition of "the same
signature" when talking about virtual functions. So, I don't really
understand the "But" in the beginning of your reply.

V
Jul 23 '05 #7

P: n/a
Steven T. Hatton wrote:
Greg wrote:
But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that
the return type that it does declare is a virtual subclass of the
return type of the function declared in the base. [assuming
compilation with a C++ compiler that supports "covariant return
types"]

Greg


Care to provide ISO/IEC 14882:2003 chapter and verse on that? Not
that I doubt what you assert, I would simply like to see how it's
actually stated in the Standard.


Can't you search your copy of the Standard for "covariant"?
Jul 23 '05 #8

P: n/a
Victor Bazarov wrote:
Steven T. Hatton wrote:
Greg wrote:
But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that
the return type that it does declare is a virtual subclass of the
return type of the function declared in the base. [assuming
compilation with a C++ compiler that supports "covariant return
types"]

Greg


Care to provide ISO/IEC 14882:2003 chapter and verse on that? Not
that I doubt what you assert, I would simply like to see how it's
actually stated in the Standard.


Can't you search your copy of the Standard for "covariant"?


I don't see the part about requiring the return type to be virtual.

"The return type of an overriding function shall be either identical to the
return type of the overridden function or covariant with the classes of the
functions. If a function D::f overrides a function B::f, the
return types of the functions are covariant if they satisfy the following
criteria:
? both are pointers to classes or references to classes98)
? the class in the return type of B::f is the same class as the class in the
return type of D::f, or is an
unambiguous and accessible direct or indirect base class of the class in
the return type of D::f
? both pointers or references have the same cv-qualification and the class
type in the return type of D::f
has the same cv-qualification as or less cv-qualification than the class
type in the return type of B::f.
If the return type of D::f differs from the return type of B::f, the class
type in the return type of D::f
shall be complete at the point of declaration of D::f or shall be the class
type D. When the overriding
function is called as the final overrider of the overridden function, its
result is converted to the type returned
by the (statically chosen) overridden function (5.2.2)."

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #9

P: n/a
Steven T. Hatton wrote:
Victor Bazarov wrote:
Steven T. Hatton wrote:
Greg wrote:

But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that
the return type that it does declare is a virtual subclass of the
return type of the function declared in the base. [assuming
compilation with a C++ compiler that supports "covariant return
types"]

Greg

Care to provide ISO/IEC 14882:2003 chapter and verse on that? Not
that I doubt what you assert, I would simply like to see how it's
actually stated in the Standard.
Can't you search your copy of the Standard for "covariant"?


I don't see the part about requiring the return type to be virtual.


Come again... How can a return type be virtual?
[...]


V
Jul 23 '05 #10

P: n/a
Victor Bazarov wrote:
provided that
> the return type that it does declare is a virtual subclass of the
> return type of the function declared in the base.


Sorry, my brain has an auto delete feature that tends to remove words from
my train of thought before it becomes committed in writing. It's the term
"virtual subclass". I take that to mean the base class was declared
virtual. It's not the wording I would have used, but it's the only meaning
I can derive from the expression.

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #11

P: n/a

Victor Bazarov wrote:
Greg wrote:
Victor Bazarov wrote:
ve*********@hotmail.com wrote:
> 1. Whats the difference between overloading and overriding?
overloading is the definition of several functions with the same
name but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);

Overloading _only_ applies to names of functions in the *same scope*.

overriding is writing a different body (in a derived class) for a
function defined in a base class.

... and *only* if the function in the base class is declared
_virtual_ and *only* if the function in the derived class has the
same signature.
class base
{
public:
int Foo()
{
return 1;
}
};

class derived: public base
{
public:
int Foo()
{
return 2;
}
};

Here you have an example of name hiding since 'base::Foo' isn't
virtual.

> 2. When is one preferable to use as opposed to the other?

They are not exclusive but complimentary. It is possible to have
several overloaded functions in a base class that are overridden in
a derived class.

This is incorrect. All functions in the derived class _hide_ the
base class functions with the same name, except in the case of
virtual ones, which override the base class functions with the
_same_signature_.


But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that
the return type that it does declare is a virtual subclass of the
return type of the function declared in the base. [assuming
compilation with a C++ compiler that supports "covariant return
types"]


Yes. Covariant return types are part of the definition of "the same
signature" when talking about virtual functions. So, I don't really
understand the "But" in the beginning of your reply.

V


The "but" was to point out a special exception to the claim that the
override function needed to have the same [which I read as "identical"]
signature as the overriden function in the base [a function's signature
includes its return type, whether the return type is considered
significant or not]. The signatures of a function and its override need
not be the same in every case, they need only be compatible to the
extent that the covariant return type rule allows.

A much more interesting discussion regarding covariant return types
would be to consider functions that return smart pointers parameterized
on base and derived types. Would it make sense to have the derived
smart pointer virtually inherit from the base smart pointer in order to
be able to return one for the other in an overridden virtual function?
Why or why not? [Actually I haven't thought the question through
myself].
Greg

Jul 23 '05 #12

P: n/a
Greg wrote:
The "but" was to point out a special exception to the claim that the
override function needed to have the same [which I read as "identical"]
signature as the overriden function in the base [a function's signature
includes its return type, whether the return type is considered
significant or not]. The signatures of a function and its override need
not be the same in every case, they need only be compatible to the
extent that the covariant return type rule allows.

A much more interesting discussion regarding covariant return types
would be to consider functions that return smart pointers parameterized
on base and derived types. Would it make sense to have the derived
smart pointer virtually inherit from the base smart pointer in order to
be able to return one for the other in an overridden virtual function?
Why or why not? [Actually I haven't thought the question through
myself].
Greg

Not that I knew the answer before looking it up, but the standard does not
state that the return type is part of a function's signature:

1.3.10 signature
[defns.signature]
the information about a function that participates in overload resolution
(13.3): the types of its parameters and, if the function is a class member,
the cv-qualifiers (if any) on the function itself and the class in which
the member function is declared. The signature of a function template
specialization includes the types of its template arguments (14.5.5.1).

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #13

P: n/a

Steven T. Hatton wrote:
Greg wrote:
But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that the
return type that it does declare is a virtual subclass of the return
type of the function declared in the base. [assuming compilation with a
C++ compiler that supports "covariant return types"]

Greg


Care to provide ISO/IEC 14882:2003 chapter and verse on that? Not that I
doubt what you assert, I would simply like to see how it's actually stated
in the Standard.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell


I'm trying to recall exactly where in the Standard I read about this
feature. I'm pretty sure it was in Chapter 10 or thereabouts.. probably
Section 3, and I have the impression the relevant description was
pretty close, if not exactly at, paragraph 5... as best as I can
recall...

Jul 23 '05 #14

P: n/a


Victor Bazarov wrote:
Steven T. Hatton wrote:
Victor Bazarov wrote:
Steven T. Hatton wrote:
Greg wrote:

> But the signature of a virtual function override can declare a
> different return type than the function it overrides, provided that
> the return type that it does declare is a virtual subclass of the
> return type of the function declared in the base. [assuming
> compilation with a C++ compiler that supports "covariant return
> types"]
>
> Greg

Care to provide ISO/IEC 14882:2003 chapter and verse on that? Not
that I doubt what you assert, I would simply like to see how it's
actually stated in the Standard.

Can't you search your copy of the Standard for "covariant"?


I don't see the part about requiring the return type to be virtual.


Come again... How can a return type be virtual?
[...]


V


I was under the impression that the return type had to polymorphically
inherit from the return type in the base. But the actual requirement
may be only that the return type in the derived function publically
inherit from the return type in the base.

Jul 23 '05 #15

P: n/a
Steven T. Hatton wrote:
Greg wrote:
The "but" was to point out a special exception to the claim that the
override function needed to have the same [which I read as "identical"]
signature as the overriden function in the base [a function's signature
includes its return type, whether the return type is considered
significant or not]. The signatures of a function and its override need
not be the same in every case, they need only be compatible to the
extent that the covariant return type rule allows.

A much more interesting discussion regarding covariant return types
would be to consider functions that return smart pointers parameterized
on base and derived types. Would it make sense to have the derived
smart pointer virtually inherit from the base smart pointer in order to
be able to return one for the other in an overridden virtual function?
Why or why not? [Actually I haven't thought the question through
myself].
Greg

Not that I knew the answer before looking it up, but the standard does not
state that the return type is part of a function's signature:

1.3.10 signature
[defns.signature]
the information about a function that participates in overload resolution
(13.3): the types of its parameters and, if the function is a class member,
the cv-qualifiers (if any) on the function itself and the class in which
the member function is declared. The signature of a function template
specialization includes the types of its template arguments (14.5.5.1).


For the sake of argument, I'll stipulate that a function signature does
not include its return type. But then the original claim that a
function declared in a derived class that has the same name and
signature of a function declared in a base class, overrides the
function in the base class, is incomplete and not wholly accurate.

The function declared in the derived class may in fact override the
function in the base, or the declaration may not be legal at all. And
one cannot assess the legality of the declaration of the function
override, without taking into consideration the requirements imposed on
both functions' return types.

Jul 23 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.