virtual function and polymorphism

J

john sun

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

E. Robert Tisdale

john said:
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?

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

EventHelix.com

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++:

http://www.eventhelix.com/RealtimeMantra/Object_Oriented/object_design_tips.htm

http://www.eventhelix.com/RealtimeMantra/Object_Oriented/object_design_tips_2.htm

Sandeep
 
T

tom_usenet

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.

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.
IMHO, virtual function is just a little
easier to use than function pointer.

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

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
 
J

jeffc

john sun said:
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?

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

john sun

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
 
D

Donovan Rebbechi

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.

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,
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top