1

Possible Duplicate:
How can I simulate OO-style polymorphism in C?

I am newbie to C, C++ trying to achieve polymorphism in C (which is not supported by C) but is there any way to do it?

Where does my code go wrong? I have seen the code on this site which was bit complex for me to understand so modified it but its not working. Sorry if the question seems to be very basic or even foolish.

#include <stdio.h> void tripple() { printf("in tripple"); } void square() { printf("\nin sq"); } int main() { void *al; al=&tripple; (*al)(); al=&square; (*al)(); return 0; } 
5
  • 2
    sorry,maybe i am wrong here...i cannot see polymorphism here...you are just assigning function pointers and calling them... Commented Jan 26, 2012 at 18:20
  • @navin isn't polymorphism "same name different task"? Commented Jan 26, 2012 at 18:34
  • @navin wiki says "polymorphism in the context of object-oriented programming, is the ability to create a variable, a function, or an object that has more than one form...." Commented Jan 26, 2012 at 18:50
  • maybe I was not clear with my answer....IMHO polymorphism or subtype polymorphism is a concept of OOPS...you can maybe implement them in C...but the concept of polymorphism is itself of OOPS...(please correct me if am wrong) Commented Jan 26, 2012 at 19:11
  • @navin you are right Concept of Polymorphism is of OOP which is better implemented in c++ than c, but this doesn't mean that C cant implement it,defiantly it can but in much harder way.to my knowledge everything can be done in C after all its closest to Machine(if not as much as Assemble.) Commented Jan 26, 2012 at 19:20

3 Answers 3

4

Frankly, you will do best to defer trying to achieve polymorphism in C until you are no longer a newbie at programming in C.

Your code is dubious (it doesn't compile!). That should be void (*al)(void); in your main and you should arguably include the void in the argument lists of tripple and square. You don't need the & in front of the function names in the assignments to al, though I don't think it does any actual harm. (Beware though; there is a difference between using an array name and the address of an array name! That is: char a[10]; char *s = a; char (*t)[10] = &a;) You should also include a newline at the end of each message in each of these functions. Newlines at the beginning of a message are often (but not always) indicative of problems. Sometimes, it is OK to omit the newline at the end of a message, but not very often.

Any polymorphism implemented in C will use function pointers. But you should not be trying to implement polymorphism in C until you are comfortable using function pointers without attempting polymorphism. I suppose it could be said to be 'learning to swim by jumping in at the deep end', but you'd do better to learn a language like C++ that supports polymorphism than trying to do it in C which doesn't really do so.

Sign up to request clarification or add additional context in comments.

3 Comments

"Newlines at the beginning of a message are often (but not always) indicative of problems" can you please elaborate ?
In the example, the newline at the beginning of the message in square() is necessary because the previous message from tripple() did not finish with a newline; if it is omitted, then the "in sq" will follow immediately after the "in tripple" string. If tripple() ended with a newline, the leading newline in square() would be unnecessary unless you wanted a blank line in the output. There should be a newline at the end of the message in tripple(). The <stdio.h> system usually flushes output when a newline is sent; data without a newline after it can remain unprinted for a while.
He just means that newlines at the beginning of the message are very rare in practice, and might be an indication that whoever wrote the code is not very good at C. printf is buffered and if there's not a newline at the end of the message it won't output anything until later (typically at program exit)
3

To solve your immediate problem, you need to type al as a function pointer. Instead of void *al, use void (*al)(void).

void* is a pointer to a value of unknown type, so can't be executed as a function. void (*al)(void). Is a pointer to a function with no arguments, which returns nothing.

Comments

1

Take a look at how the VTABLE and the VPTR are used in C++ to implement polymorphic virtual function calls.

A good reading on the subject can be found on Thinking In C++ Vol.1, by Bruce Eckel.

Reference: How C++ implements Late Binding, page 636, Thinking In C++ (the book is freely available in PDF)

Link to download page: http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

2 Comments

If the book is readily available, it is sensible to provide the URL in the answer.
Can you please explain the downvote? He asked: "I am newbie to C, C++ trying to achieve polymorphism in C (which is not supported by C) but is there any way to do it?" What's wrong in answering "if you want polymorphism in C, implement it as it is in C++"? That approach would work. The fact that using C++ would be simpler is an answer to a different question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.