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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:41:52+00:00 2026-06-02T06:41:52+00:00

Given the following block of code: class BaseClass { public: virtual void hello() {

  • 0

Given the following block of code:

class BaseClass
{
public:
    virtual void hello() { cout << "Hello from Base" << endl; }
};

class DerivedClass : public BaseClass
{
public:
    void hello() { cout << "Hello from Derived" << endl; }
};

int main()
{
    BaseClass base;
    DerivedClass derv;

    BaseClass* bp = &base;
    bp->hello();
    bp = &derv;
    bp->hello();
}

How is the type that bp is pointing at determined at runtime? I understand it is dynamically bound, but what is the mechanism that does so?
I’m confused because typically the answer is the compiler, however because it is dynamic, it is not the case in this example (or am I mistaken? I assume the compiler this up ahead of time, but what indicates that bp now points to a DerivedClass?). I’m also coming from C#, so this idea is foreign to me seeing as this is native code without the CLR.

  • 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-06-02T06:41:53+00:00Added an answer on June 2, 2026 at 6:41 am

    When the DerivedClass is constructed, an invisible member is inserted into it’s data. That member points to something called a vtable. The vtable has function pointers to the virtual function implementations of the derived class.

    Each concrete class (a class which you can instantiate) has it’s own vtable somewhere in memory. These tables only get generated if you have virtual functions, part of C++’s motto about not paying for things you don’t use.

    When the compiler sees bp->hello(), it knows to look for that vtable pointer, and calls the correct function.

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

Sidebar

Related Questions

Given the following code... class Program { static void Main(string[] args) { Foo foo
Consider the following code :- public class UsingWait1{ public static void main(String... aaa){ CalculateSeries
Given the following code: void f() { class A { template <typename T> void
I have the following code block: - using System.ComponentModel.DataAnnotations; public class NewsItem { [RegularExpression(System.Configuration.ConfigurationSettings.AppSettings[UrlRegEx],
given i have the following block of code (function(){ var mb = { abc:function(){
Given the following code, is there a way I can call class A's version
following snippet is from rails code def rescue_from(*klasses, &block) options = klasses.extract_options! unless options.has_key?(:with)
Given the following sample form code and CSS stylesheet Form <div class="Center_300 "> <button
I have the following java code: public class CheckInnerStatic { private static class Test
Given the following simplified code block in ASP.NET <% foreach( item in Model) {

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.