function overload vs. virtual function

V

Vaxius

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).
 
D

David W

Thomas J. Gritzan said:
Please repeat the question in the body of the posting:
"function overload vs. virtual function"


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
 
J

Jim Langston

asdf said:
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.
 
S

Sumit Rajan

Jim said:
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.
 
T

Thomas J. Gritzan

David said:
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. :)
 
D

David Harmon

On 9 Oct 2006 19:37:36 -0700 in comp.lang.c++, "asdf"
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top