Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 868493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:08:54+00:00 2026-05-15T10:08:54+00:00

The C++ language provides virtual functions. Within the constraints of a pure C language

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:08:55+00:00Added an answer on May 15, 2026 at 10:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So if a language provides higher order procedure then I can have procedure that
Is there any super set or language similar to javascript that provides a more
In an object-oriented language with inheritance and virtual functions, removing dependencies (e.g. database, API
Is there any statically-typed, strongly-type compiled language that provides a functionality to iterate over
I notice that Swig provides a whole host of functions to allow for typecasting
I want know which programming language provides good number of libraries to program a
Here is a language translation code that google provides to detect the language in
Is there any tool that provides language recognition functionality? Like if I input a
I'm looking for a Java macro language that provides for convenient ways of doing
Is there a structured language for declaring styles in a sensible way, which can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.