473,226 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

function overload vs. virtual function

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

Oct 10 '06 #1
7 6054
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
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
"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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
3
by: Piotre Ugrumov | last post by:
I have declared some function virtual, but when I call this function I have errors. In a class Simulator I have defined this: Nave *navi; The virtual function is the function modifica(). I call...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
4
by: rahul8143 | last post by:
hello, what happens to VTABLE when base class has virtual function and derived class does not override it? Does derived class also builds VTABLE if it has no functions? eg. if code is like...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
5
by: Squeamizh | last post by:
The subject line probably isn't the best, but I don't know how else to describe my question. When I try to compile the code below, my compiler reports an error (error text is below code sample)....
2
by: Fred | last post by:
I've got the following code: #include <iostream> class Base{ private: virtual void f(int) { std::cout << "f in Base" << std::endl; } };
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.