Solved

polymorphism  using C

Posted on 2002-03-13
10
230 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ dll will not run on machine without VS installed 10 86
C++ error cannot convert from std::string to const char * 6 184
Header of docx file 17 102
Precision Problem in C++ 7 32
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

770 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