Solved

polymorphism  using C

Posted on 2002-03-13
10
231 Views
Last Modified: 2010-04-01
Hi,

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

0
Comment
Question by:govar
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 200 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

829 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