20

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

20
  • 2
    why do you want to implement C++ again? Topics like this can be easily found in books and there are lot of online articles as well. By asking these kind of questions, your reputation will be affected and people will start to take you for granted. So do all you can to find the answer, and still can not then ask :). Commented Jun 24, 2010 at 20:20
  • 3
    Sounds like your teacher needs to take a break from academia and get a real programming job. Commented Jun 24, 2010 at 20:26
  • 5
    if this is a not real question why there exits alotof answer,whY??? Commented Jun 24, 2010 at 21:01
  • 1
    I've tried to improve the wording of the question to make it clearer that it could be reopened and get instructive and useful answers. Commented Jun 24, 2010 at 21:52
  • 1
    Searching [[c] object oriented](://stackoverflow.com/search?q=[c]+object+oriented) yields: stackoverflow.com/questions/3072499/… stackoverflow.com/questions/351733/… stackoverflow.com/questions/1201521/… stackoverflow.com/questions/2181079/… stackoverflow.com/questions/415452/object-orientation-in-c of which the one Roger posted has the best answer to this version of the questions. Commented Jun 25, 2010 at 21:34

4 Answers 4

35

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.

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

6 Comments

+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.
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.
I don't think A::access() is present in the virtual table as this is a non-virt function.
Just a note: the link in the beginning is broken.
|
1

@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

1 Comment

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

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.)

Comments

0

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.

10 Comments

actually, there is a notion of inheritance in C but it does not respect ACCESS control here : stackoverflow.com/questions/577465/…
@Gollum: A C++ struct is not like C struct.
You can't do it directly (class is missing :)) - but it's no problem to implement such a system using structures and function pointers.
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.
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.
|