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

  • Home
  • SEARCH
  • 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 8367141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:05:08+00:00 2026-06-09T13:05:08+00:00

Given a base class A , with the A::get and A::add functions defined. class

  • 0

Given a base class A, with the A::get and A::add functions defined.

class A { 
public:
  int get() { return 3; }
  int add() {
    return get() + get();
  }
};

Note that A::add calls A::get. Is there a way a derived class B can call A::add but use it’s own B::get function? Something like:

class B : public A {
  int get() { return 7; }
};

#include <iostream>

int main() {
  A foo;
  std::cout << foo.add() << std::endl;

  B bar;
  std::cout << bar.add() << std::endl;
  return 0;
}

Where the expected output is 14 not 6 in the second case. If had control over A, I could make ::get a virtual function and each derived class could implement it as needed. However, lets assume that A is immutable – how do I call the correct ::get?

  • 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-09T13:05:10+00:00Added an answer on June 9, 2026 at 1:05 pm

    No, this is not possible: imagine that A is compiled separately and given to you as a pre-compiled library to see why.

    Since A::get is not virtual, the compiler inserts a call straight to A::get, without any indirection through vtable, into its output for A::add. Moreover, the compiler could even inline the call if it chooses to do so, “baking in” the return of 3 into its output.

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

Sidebar

Related Questions

Given a base class with the following interface: public class Base { public virtual
Given the sample code: class Base { public: bool pub; protected: bool prot; };
I have a simple class as defined below: public class Person { int _id;
Given the base class A and the derived class B: class A { public:
In other words, given a base class shape and a derived class rectangle :
given a Python class hierarchy, say class Base: def method1 def method2 def method3
Given this class: class MyModel < ActiveRecord::Base belongs_to :association1 belongs_to :association2, :polymorphic => true
Given the following models: team.rb class Team < ActiveRecord::Base has_many :events, :dependent => :destroy
Given the following statements in VBA: Assume that val = 4 and Option Base
Given a Queue<MyMessage> , where MyMessage is the base class for some types of

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.