virtual function and polymorphism

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

    virtual function and polymorphism

    Hello,

    I am not newbie C++ developer infact :). But till recently I would like dig
    up more about C++.
    I know those OO details. But when people talking OO they focused on the
    polymorphism, and think the "old code" can call "new code" is the biggest OO
    feature. But as we know, in C we also can use function pointer to do the
    same thing with almost same effort. IMHO, virtual function is just a little
    easier to use than function pointer. If C++ FAQ Lite said non-virtual member
    function is only suger, ( I agree), then I tend to believe that the virtual
    function is well-wrapped suger. Did I miss something here?

    Thanks very much for discussion!

    John


  • E. Robert Tisdale

    #2
    Off Topic: virtual function and polymorphism

    john sun wrote:
    [color=blue]
    > I am not newbie C++ developer in fact :).
    > But till recently I would like dig up more about C++.
    > I know those OO details. But, when people talking OO,
    > they focused on the [run-time] polymorphism and
    > think the "old code" can call "new code" is the biggest OO feature.
    > But, as we know, in C, we also can use function pointer
    > to do the same thing with almost same effort. IMHO, virtual functions
    > are just a little easier to use than function pointers.
    > If C++ FAQ Lite said non-virtual member function is only sugar,
    > (I agree), then I tend to believe that the virtual function
    > is well-wrapped sugar. Did I miss something here?[/color]

    Yes.
    You should actually try to implement run-time polymorphism in C.
    Then, I believe, you will understand why having the feature
    built into the language is so important.
    It really requires [super human] self-discipline to get it right
    and make it portable.

    But this discussion is probably off-topic
    in the comp.lang.c++ newsgroup.
    You might try the comp.object or comp.lang.c newsgroup instead.

    Comment

    • EventHelix.com

      #3
      Re: virtual function and polymorphism

      Polymorphism and virtual functions are just one of the advantages of
      C++ over C. There are several others.

      Just having the ability to define classes and the encapsulation it
      provides would probably rank higher.

      As far as supporting polymorphism goes, C++ provides a simple mechanism.
      With C you have to jump through hoops to get the same functionality
      via function pointers.

      Try writing an application that uses polymorphic behavior in C and you will
      see the difference.

      The following articles will help you in getting started with C++:





      Sandeep
      --
      Sequence diagram based systems engineering and architecture design tool. Built in support for alternative scenarios and multi-tier architectures.

      EventStudio 2.0 - System Architecture Design CASE Tool

      Comment

      • tom_usenet

        #4
        Re: virtual function and polymorphism

        On Thu, 22 Jan 2004 00:38:59 GMT, "john sun" <johnsun@sbcglo bal.net>
        wrote:
        [color=blue]
        >Hello,
        >
        >I am not newbie C++ developer infact :). But till recently I would like dig
        >up more about C++.
        >I know those OO details. But when people talking OO they focused on the
        >polymorphism , and think the "old code" can call "new code" is the biggest OO
        >feature. But as we know, in C we also can use function pointer to do the
        >same thing with almost same effort.[/color]

        The first C++ compiler, CFront, generated C code as its output. No
        doubt it tables of function pointers for polymorphism in the generated
        C code. Of course you can implement vtables in C code.
        [color=blue]
        >IMHO, virtual function is just a little
        >easier to use than function pointer.[/color]

        A lot easier when it comes to class heirarchies, etc. C code becomes
        an error-prone mess in those circumstances, with a misplaced cast
        causing hard to find bugs. The equivalent C++ code is very hard to get
        wrong, since the compiler will complain if you make a mistake in most
        cases.
        [color=blue]
        >If C++ FAQ Lite said non-virtual member
        >function is only suger, ( I agree), then I tend to believe that the virtual
        >function is well-wrapped suger. Did I miss something here?[/color]

        No, but this sugar is extremely important. In C the overhead of
        implementing polymorphism is too great for it to be commonly employed.
        Also, each implementation is bespoke so different solutions can't
        interact without implementing lots of glue code. In C++ none of these
        problems exist.

        Tom

        C++ FAQ: http://www.parashift.com/c++-faq-lite/
        C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

        Comment

        • jeffc

          #5
          Re: virtual function and polymorphism


          "john sun" <johnsun@sbcglo bal.net> wrote in message
          news:DYEPb.2721 0$oq4.9501@news svr33.news.prod igy.com...[color=blue]
          > Hello,
          >
          > I am not newbie C++ developer infact :). But till recently I would like[/color]
          dig[color=blue]
          > up more about C++.
          > I know those OO details. But when people talking OO they focused on the
          > polymorphism, and think the "old code" can call "new code" is the biggest[/color]
          OO[color=blue]
          > feature. But as we know, in C we also can use function pointer to do the
          > same thing with almost same effort. IMHO, virtual function is just a[/color]
          little[color=blue]
          > easier to use than function pointer. If C++ FAQ Lite said non-virtual[/color]
          member[color=blue]
          > function is only suger, ( I agree), then I tend to believe that the[/color]
          virtual[color=blue]
          > function is well-wrapped suger. Did I miss something here?[/color]

          No, it's just a matter of degree. Most people would disagree with you that
          it's "just a little" easier to use than function pointers. Most people seem
          to think it's "quite a bit easier". To take your point further, couldn't
          you also say that the C programming language is just well-wrapped sugar over
          machine code? So, why don't you just program in machine code?


          Comment

          • john sun

            #6
            Re: virtual function and polymorphism

            Thanks all for discussion.
            I thought just using one function pointer is not difficult in class. But in
            real case, if implemented the class hirarchy based virtual function table
            should be a lot of difficult.

            Thanks again!

            John

            "john sun" <johnsun@sbcglo bal.net> wrote in message
            news:DYEPb.2721 0$oq4.9501@news svr33.news.prod igy.com...[color=blue]
            > Hello,
            >
            > I am not newbie C++ developer infact :). But till recently I would like[/color]
            dig[color=blue]
            > up more about C++.
            > I know those OO details. But when people talking OO they focused on the
            > polymorphism, and think the "old code" can call "new code" is the biggest[/color]
            OO[color=blue]
            > feature. But as we know, in C we also can use function pointer to do the
            > same thing with almost same effort. IMHO, virtual function is just a[/color]
            little[color=blue]
            > easier to use than function pointer. If C++ FAQ Lite said non-virtual[/color]
            member[color=blue]
            > function is only suger, ( I agree), then I tend to believe that the[/color]
            virtual[color=blue]
            > function is well-wrapped suger. Did I miss something here?
            >
            > Thanks very much for discussion!
            >
            > John
            >
            >[/color]


            Comment

            • Donovan Rebbechi

              #7
              Re: virtual function and polymorphism

              In article <b8lv00dvgupm47 qk0ihakeofqvjmp p1i4r@4ax.com>, tom_usenet wrote:[color=blue]
              > On Thu, 22 Jan 2004 00:38:59 GMT, "john sun" <johnsun@sbcglo bal.net>
              > wrote:[/color]
              [color=blue][color=green]
              >>IMHO, virtual function is just a little
              >>easier to use than function pointer.[/color]
              >
              > A lot easier when it comes to class heirarchies, etc. C code becomes
              > an error-prone mess in those circumstances, with a misplaced cast
              > causing hard to find bugs. The equivalent C++ code is very hard to get
              > wrong, since the compiler will complain if you make a mistake in most
              > cases.[/color]

              And vtables are where the mess starts, not where it ends. Destructors
              actually give you a lot.

              Writing exception safe polymorphic code in C (or even code that handles
              multiple execution paths safely) is quite messy. One often needs the
              mini "cleanup blocks" with gotos for a given function (that or very repetitive
              error prone code, or specialised cleanup helper-functions) to deallocate
              various dynamically allocated structures prior to making a safe exit.

              Cheers,
              --
              Donovan Rebbechi

              Comment

              Working...