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 6793859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:09:07+00:00 2026-05-26T18:09:07+00:00

I am working on an assignment on compiler design. In the code generation part

  • 0

I am working on an assignment on compiler design. In the code generation part I am stuck with how to create instructions which will ensure that appropriate method is called at run time. The language is a very small subset of C++.

let’s say:

void main()
{
  Animal* a;
  a = new Cow;
  //what code should be generated to ensure that object 'a' calls Cow::Init here
  a->Init(5);
}

class Cow : public Animal{
 void Init(int h)
 {
   height = h;
 }
}

class Animal {
 int height; 
 virtual void Init(int h){
   height = h;
  }
}
  • 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-26T18:09:08+00:00Added an answer on May 26, 2026 at 6:09 pm

    a very simple way of doing this(note: this excludes optimizing for know calls at compile time):
    if your class has any virtual members (including inherited), then its very first member becomes a pointer to a vftable. the vftable is constant per class definition which is why you need only a pointer.

    from there, each unique function is assigned an index in that vftable, so each unique name (note: by name I mean the symbol name including types, but no class-namespace qualification) has a unique index, then the table is filled in from the class at the very top of the inheritance tree down to your current working class definition.

    In doing so, newer redifinitions of virtual function will overwrite the older entries that share their index. calling the functions then becomes trivial as you just generate a call to the index for that function’s name-index.

    So in your example, Animal has a vftable with 1 entry, Init(int), which is assigned the unique index of 0. so you have a vftable looking like so:

    ;Animal - vftable
    &Animal::Init //note: this isn't a class member pointer in the C++ sense, its a namespaced function pointer if you will
    

    then when you build the vftable for Cow, you use Animals as a base and add in the virtual functions, in this case Init(int), but it already has a unique index of 0, so we overwrite the function at index 0:

    ;Cow - vftable
    &Cow::Init
    

    then if we have the call:

    a->Init(5);
    

    we simply transform that to:

    a->vftable[0](5);
    

    where 0 was the unique index allocated to Init(int).

    an assembly example just in case that helps:

    ;ecx contains our class pointer
    mov eax,[ecx] ;get the vftable ptr
    mov eax,[eax] ; get the ptr at (vftable + (unique_index * sizeof(func_ptr)))
    push 5 ;push our arg 5, ecx is already setup for __thiscall
    call eax ; let it rip!
    

    note: this all assumes your symbol table is setup to be able to detect virtual functions passed through inheritance or those that become virtual from inheritance.


    If this where to be optimized you could analyze a and find that its only assigned a value once, thus you can morph its class to the class of the value it was assigned, Cow. then seeing as you have a class at the end of a derivation chain, you can fold away the vftable call and use a call directly to Cow::Init, how this is a lot more tricky, and there are many ways of optimizing out vftable calls, for a project it shouldn’t matter.

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

Sidebar

Related Questions

I'm working on a homework assignment (a project), for which one criterion is that
I'm working on assignment here, part of which requires me to parse integers from
I'm working on an assignment that is telling me to assume that I have
I am working on an assignment for networking where we are supposed to create
I'm currently working through an assignment that deals with Big-O and running times. I
I'm working on a class assignment that started small, so I had it all
I'm working on a homework assignment in which I'm required to use char arrays
I'm currently working on an assignment and this have had me stuck for hours.
I'm working on a assignment for class so this is only part of the
The code I'm working with has its own smart pointer implementation which does simple

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.