function overload vs. virtual function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • asdf

    #1

    function overload vs. virtual function

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

  • Vaxius

    #2
    Re: function overload vs. virtual function

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

    Comment

    • Thomas J. Gritzan

      #3
      Re: function overload vs. virtual function

      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_?



      --
      Thomas

      Comment

      • David W

        #4
        Re: function overload vs. virtual function

        "Thomas J. Gritzan" <Phygon_ANTISPA M@gmx.dewrote in message
        news:egf287$14h $1@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


        Comment

        • Jim Langston

          #5
          Re: function overload vs. virtual function

          "asdf" <likemursili@gm ail.comwrote in message
          news:1160447856 .541366.76400@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.


          Comment

          • Sumit Rajan

            #6
            Re: function overload vs. virtual function

            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.

            Comment

            • Thomas J. Gritzan

              #7
              Re: function overload vs. virtual function

              David W wrote:
              "Thomas J. Gritzan" <Phygon_ANTISPA M@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

              Comment

              • David Harmon

                #8
                Re: function overload vs. virtual function

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

                Comment

                Working...