?
Solved

polymorphism  using C

Posted on 2002-03-13
10
Medium Priority
?
236 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
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!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

762 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