Solved

polymorphism  using C

Posted on 2002-03-13
10
228 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
Comment Utility
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
Comment Utility
polymorphism only works with object-oriented languages, one of which C isn't. Try C++ instead.
0
 

Expert Comment

by:0xDEADBEEF
Comment Utility
@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
 

Expert Comment

by:0xDEADBEEF
Comment Utility
@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
Comment Utility
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Expert Comment

by:lidorc
Comment Utility
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
Comment Utility
0
 
LVL 5

Expert Comment

by:yonat
Comment Utility
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now