function overload vs. virtual function

Discussion in 'C++' started by asdf, Oct 10, 2006.

  1. asdf

    asdf Guest

    They looks so similar. Anybody would like to tell me their differences?
    Thanks a lot.
     
    asdf, Oct 10, 2006
    #1
    1. Advertising

  2. asdf

    Vaxius Guest

    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).
     
    Vaxius, Oct 10, 2006
    #2
    1. Advertising

  3. Thomas J. Gritzan, Oct 10, 2006
    #3
  4. asdf

    David W Guest

    "Thomas J. Gritzan" <> wrote in message
    news:egf287$14h$...
    > 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
     
    David W, Oct 10, 2006
    #4
  5. asdf

    Jim Langston Guest

    "asdf" <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Oct 10, 2006
    #5
  6. asdf

    Sumit Rajan Guest

    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.
     
    Sumit Rajan, Oct 10, 2006
    #6
  7. David W wrote:
    > "Thomas J. Gritzan" <> wrote:
    >> 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
     
    Thomas J. Gritzan, Oct 10, 2006
    #7
  8. asdf

    David Harmon Guest

    On 9 Oct 2006 19:37:36 -0700 in comp.lang.c++, "asdf"
    <> wrote,
    >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.)
     
    David Harmon, Oct 11, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    395
    Nick Hounsome
    Jan 25, 2004
  2. IK
    Replies:
    2
    Views:
    639
    hemraj
    Jul 23, 2004
  3. Allen
    Replies:
    2
    Views:
    710
    Rolf Magnus
    Aug 27, 2008
  4. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    281
    Sherm Pendley
    Oct 11, 2004
  5. Lloyd
    Replies:
    10
    Views:
    302
    Victor Bazarov
    Aug 2, 2013
Loading...

Share This Page