views:

297

answers:

4

Possible Duplicate:
Object oriented programming in C

The C++ language provides virtual functions. Within the constraints of a pure C language implementation, how can a similar effect be achieved?

A: 

Here is a description of what virtual functions are.

There is no way to implement virtual functions in plain C, because C has no notion of inheritance.

Update: As is discussed in the comments below, it is possible to do something similar to virtual functions in straight C using structures and function pointers. However, if you are accustomed to a language like C++ that has "true" virtual functions, you will probably find the C approximation far less elegant and harder to use.

bta
actually, there is a notion of inheritance in C but it does not respect ACCESS control here : http://stackoverflow.com/questions/577465/in-c-can-i-derive-a-class-from-a-struct
Gollum
@Gollum: A C++ struct is not like C struct.
Alerty
You can't do it directly (class is missing :)) - but it's no problem to implement such a system using structures and function pointers.
ManniAT
Note that it is (usually) possible to interoperate with C++ virtual functions from C, if enough is known about the C++ ABI. For example, Windows COM uses virtual functions to declare interfaces, and there is a technique (made just barely usable by a lot of macros) for declaring COM interfaces and calling through them from C. Also, C++ started life (with many of its current features including virtual functions) as a preprocessor that read C++ text and wrote C. So it *is* possible to implement them in C, just not pleasant.
RBerteig
That question is on C++ which has inheritance. All you can do in C is simulate it by storing pointers to your functions on the structures you're passing around.
jer
he said when we implement phyton in c we use some way ;'if you use same way ,you will achieve'
gcc
@RBerteig: but, of course, there is no standard C++ ABI, so any C code which gets cozy with C++ virt funcs will be unportable. So if you want a portable, dynamically-linkable virtual function interface (like COM), you have to at least act like you're using C.
Tim Schaeffer
@Alerty, really ? I did not know that can you elaborate?
Gollum
@Tim, true. And COM was defined in a way that "coincidentally" allowed an Interface to be represented as an array of pointers to functions in C and as a virtual function table in Visual C++. This was acceptable because portability was not a priority for Microsoft. But remains an example of one way that virtual functions can be implemented in and called from C.
RBerteig
@Gollum: It is possible in C to have functions in structs. One has to use a function pointer and set it (this would be rather ugly). Also, please note that there is no public/private/protected keyword in C. In C++, a struct is the same as a class except that everything is public by default.
Alerty
A: 

Virtual functions are a feature of C++'s object orientation. They refer to methods that depend on a specific object instance rather than what type you're currently carrying them around as.

In other words: if you instantiate an object as Bar, then cast it to Foo, virtual methods will still be the ones they were at instantiation (defined in Bar), while other methods will be the ones from Foo.

Virtual functions are typically implemented by way of vtables (that's for you to do more research on ;)).

You can simulate similar things in C by using structs as poor man's objects and saving function pointers in them.

(More correctly, non-virtual functions make it ambiguous which class the method should be taken from, but in practice I believe C++ uses the current type.)

Jan Krüger
+15  A: 

Stolen from here.

From the C++ class

class A {
protected:
    int a;
public:
    A() {a = 10;}
    virtual void update() {a++;}
    int access() {update(); return a;}
};

a C code fragment can be derived. The three C++ member functions of class A are rewritten using out-of-line (standalone) code and collected by address into a struct named A_functable. The data members of A and combined with the function table into a C struct named A.

struct A;

typedef struct {
    void (*A)(struct A*);
    void (*update)(struct A*);
    int (*access)(struct A*);
} A_functable;

typedef struct A{
    int a;
    A_functable *vmt;
} A;

void A_A(A *this);
void A_update(A* this);
int A_access(A* this);

A_functable A_vmt = {A_A, A_update, A_access};

void A_A(A *this) {this->vmt = &A_vmt; this->a = 10;}
void A_update(A* this) {this->a++;}
int A_access(A* this) {this->vmt->update(this); return this->a;}

/*
class B: public A {
public:
    void update() {a--;}
};
*/

struct B;

typedef struct {
    void (*B)(struct B*);
    void (*update)(struct B*);
    int (*access)(struct A*);
} B_functable;

typedef struct B {
    A inherited;
} B;

void B_B(B *this);
void B_update(B* this);

B_functable B_vmt = {B_B, B_update, A_access};

void B_B(B *this) {A_A(this); this->inherited.vmt = &B_vmt; }
void B_update(B* this) {this->inherited.a--;}
int B_access(B* this) {this->inherited.vmt->update(this); return this->inherited.a;}

int main() {
    A x;
    B y;
    A_A(&x);
    B_B(&y);
    printf("%d\n", x.vmt->access(&x));
    printf("%d\n", y.inherited.vmt->access(&y));
}

More elaborate than necessary, but it gets the point across.

Tim Schaeffer
+1 Great example: this is about as close as you can get to translating vtables to C and is far more elegant than C programmers doing things like function pointer casts or casting structures of function pointers. It's still bulky and awkward, but hey, C wasn't designed for this.
@stinky472: I agree with you, but when someone is at the point of needing code like this it just makes no sense. Certain languages are better suited for certain problems.
Alerty
Very true, but I have been forced to work in C systems trying to implement OOP. They did it by casting structures with function pointers (like casting struct A), rather than just passing A for polymorphism and allowing every subclass-like struct to simply store A and assign appropriate function addresses and data to it. This, at least, is far more elegant than struct casting or casting function pointers.
A: 

@GCC....A virtual function is declared in the Base class of an object and is then "overriden" or implemented in the sub classes. i.e., say you have Vehicle Base class and you create two sub-classes, Motorcycle and, Automobile. The Base class would declare a virtual function of AddTires() Then the Sub Classes would implement this function and each sub class would implement it differently. A car has 4 wheels, where a motorcycle has 2. I can't give you the syntax for C or C++, though. Hope this helps

MikeTWebb
Of course, my answer is based on C# and that object model
MikeTWebb