473,569 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6112
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*********@gm ail.comwrote in message
news:11******** *************@m 7g2000cwm.googl egroups.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*********@gm ail.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
8082
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 how and why it works. I'll post a simplified version of it below and ask my questions afterwards: class Base { void *function_ptr;
3
423
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 this function in this way: navi->modifica(); In this way, the function work correctly. But in another class Simulator I have defined: Veicolo...
16
16223
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
3029
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 this(only demo code) class a{ public: virtual void fun() { cout<<"Base class";
2
2502
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 pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter...
6
3458
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
2380
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). class GrandParent { virtual void doSomething() = 0; };
2
1486
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
2733
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
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.