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

function overload vs. virtual function

P: n/a
They looks so similar. Anybody would like to tell me their differences?
Thanks a lot.

Oct 10 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Mon, 09 Oct 2006 19:37:36 -0700, asdf wrote:
They looks so similar. Anybody would like to tell me their differences?
Thanks a lot.
As I understand it, virtual functions refer to the use of a function that
has overriden a function of the same name that was inherited from another
class. Overloading functions refers to creating multiple functions with
the same name, but ultimately have different signatures because each one
takes a different set of arguments (or maybe none at all).

Oct 10 '06 #2

P: n/a
Please repeat the question in the body of the posting:
"function overload vs. virtual function"

asdf wrote:
They looks so similar. Anybody would like to tell me their differences?
This question makes no sense. Do you confuse overloading with over_writing_?

http://www.parashift.com/c++-faq-lit...functions.html

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 10 '06 #3

P: n/a
"Thomas J. Gritzan" <Ph*************@gmx.dewrote in message
news:eg**********@newsreader2.netcologne.de...
Please repeat the question in the body of the posting:
"function overload vs. virtual function"

asdf wrote:
They looks so similar. Anybody would like to tell me their differences?

This question makes no sense. Do you confuse overloading with over_writing_?
Confusing overloading with _overriding_ is rather more likely than confusing it with overwriting.

DW
Oct 10 '06 #4

P: n/a
"asdf" <li*********@gmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
They looks so similar. Anybody would like to tell me their differences?
Thanks a lot.
int MyFunc( int Val )
{
return Val;
}

int MyFunc( int Val, int Val2 )
{
return Val * Val2;
}

That's overloading. Same function with two different signatures. One takes
one int, one takes two ints.

class MyClass
{
vitural MyFunc( int Val ) { return Val; };
};

class MyClassDerived: public MyClass
{
MyFunc( int Val ) { return Val / 2; }
};

That's a virtual function. If you instantize MyClassDerived the
function/method declared in MyClassDerived is called.
Oct 10 '06 #5

P: n/a
Jim Langston wrote:
>
class MyClass
{
public:
vitural MyFunc( int Val ) { return Val; };
I guess you mean virtual above.
};

class MyClassDerived: public MyClass
{
MyFunc( int Val ) { return Val / 2; }
};

Besides, you haven't declared a return type for MyFunc and you're
attempting to return an int in each of the cases above.

Regards,
Sumit.
Oct 10 '06 #6

P: n/a
David W wrote:
"Thomas J. Gritzan" <Ph*************@gmx.dewrote:
>Please repeat the question in the body of the posting:
"function overload vs. virtual function"

asdf wrote:
>>They looks so similar. Anybody would like to tell me their differences?
This question makes no sense. Do you confuse overloading with over_writing_?

Confusing overloading with _overriding_ is rather more likely than confusing it with overwriting.
You are right. In german it's overwriting ("überschreiben").

To the OP:

There is function overloading, function hiding and function overriding.

It's called function overloading when you have functions with the same name
but different signatures:

void foo(int i);
void foo(float f);

The compiler decides by looking at the actual parameter types, which
function is called.

If you have two functions with the same name in two different but enclosing
scopes, it is called function hiding:

namespace outer
{
void foo(int i);

namespace inner
{
void foo(float f);
}
}

In the inner scope, only the void foo(float) gets called, because the outer
function is hidden. The same applies to non-virtual functions or virtual
function with different signature:

class base
{
virtual void foo(int i);
void bar(int i);
};

class sub : public base
{
void foo(float f); // different signature, hides base::foo
void bar(int i); // same signature as in base, but non-virtual, so
hides base::bar
};

Only if you have a virtual function with the same signature, it's called
function overriding, and the function gets called polymorphically.

Hope that's right and clear, and no homework. :-)

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 10 '06 #7

P: n/a
On 9 Oct 2006 19:37:36 -0700 in comp.lang.c++, "asdf"
<li*********@gmail.comwrote,
>They looks so similar. Anybody would like to tell me their differences?
Thanks a lot.
Virtual functions are all about making decisions at run time.

Overloading is about making the decision at compile time.

That is, the decision about exactly which function with the same
name gets called. Virtual function calls decide that based upon the
dynamic type of the actual object pointed to by a base class pointer
or reference. Which can be different, for example, every time
through a loop. Overload resolution is based on the static argument
type, determined strictly at compile time.

By the way, templates are also about making decisions at compile
time. The rule "never put off to run time what you can do at
compile time" is a good one to remember (although it's not always
true.)

Oct 11 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.