Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

polymorphism  using C

Posted on 2002-03-13
10
Medium Priority
?
237 Views
Last Modified: 2010-04-01
Hi,

How to implemet function polymorphism using C language.
thaxn in Advance.

0
Comment
Question by:govar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 1

Expert Comment

by:lidorc
ID: 6860575
Well qovar, you can do this in several ways, depends on what you really looking for.
The most simple way is to use switch in every "polymorphic" function, and pass the type of the object to the function - very simple but not very good, you'll need to add a case in the switch for every new object in every function.
The second way, in every object (actualy struct) you'll hold a pointer to a function with the specific implementation, and after that pointer, you'll have the struct data. You'll also need a struct that will act as the base class, and that struct has just the pointer to the function with the same name as the object. Once you'll have a pointer to the "base" struct you'll be able to call the funtion, with all the parameters and an extra "this" pointer, and that function knows how to use the data in the "this" pointer.
I know this sounds terrible.. sorry.
The third way is actualy the same a C++ does, with some kind of a vtable. You can read about it in every average C++ book or ask again.
I hope that's what you ment..
0
 

Accepted Solution

by:
0xDEADBEEF earned 800 total points
ID: 6860579
polymorphism only works with object-oriented languages, one of which C isn't. Try C++ instead.
0
 

Expert Comment

by:0xDEADBEEF
ID: 6860595
@lidorc: I think you're talking about member methods. polymorphism means functions using the same name while doing different stuff. afaik, C is not capable of that.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:0xDEADBEEF
ID: 6860850
@lidorc: I think you're talking about member methods. polymorphism means functions using the same name while doing different stuff. afaik, C is not capable of that.
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6860884
0
 
LVL 1

Expert Comment

by:lidorc
ID: 6861282
Hey guys..
I know C isn't an object oriented language, but govar wanted to know how to twist it and make it possible (as far as I understood).
After all, C++ can be implemented using C, in fact, the first compilers of C++, I've heard, just traslated the cpp file to a C file, and run the C compiler.
The buttom line is, govar, what do you really need? what for? we need some more details..
0
 
LVL 1

Expert Comment

by:Nick75
ID: 6861352
0
 
LVL 5

Expert Comment

by:yonat
ID: 6861443
0
 
LVL 11

Expert Comment

by:griessh
ID: 6955354
Dear govar

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     ": 0xDEADBEEF"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 6979083
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question