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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:04:58+00:00 2026-05-23T00:04:58+00:00

consider the following program: class Base { public: virtual void foo() const { cout

  • 0

consider the following program:

class Base {
public:
    virtual void foo() const {
        cout << "Base::foo()" << endl;
    }
};

class Derived : public Base {
public:
    virtual void foo() {
        cout << "Derived::foo()" << endl;
    }
};

void func(Base& obj) {
    obj.foo();
}

void main() {
    Derived d;
    func(d); // Base::foo() is printed 
}

If I remove the const from the Base class foo method then Derived::foo() is called.
I can’t seem to understand this behavior.

1) What is the reason for this behavior?

2) Is this decided at compile time or runtime?

Thanks

  • 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-23T00:04:59+00:00Added an answer on May 23, 2026 at 12:04 am

    In the derived class, the function signature is this:

    virtual void foo(); //Derived::foo
    

    which doesn’t mention const. Its a non-const member function, while the Base::foo is a const member function. They’re two different functions, because const is a part of the function signature.

    virtual void foo() const; //Base::foo 
    

    Derived class does NOT override this function, instead it adds another function.

    So the fix is this:

    class Derived : public Base {
    public:
        virtual void foo() const {
            cout << "Derived::foo()" << endl;
        }
    };
    

    As const is a part of the function signature. So you must mention it when you intend to override base’s foo.


    @davka’s asked:

    so why the const version is selected over the non-const? Is there any rule or it just happened to be the first option?

    Its because the static type of obj is Base, and function name is resolved based on the static type of object. Base doesn’t even have non-const version. So there is no question of it being selected or rejected. It doesn’t exist in Base to begin with.

    void func(Base& obj) {
        obj.foo();  //calls Base::foo
    }
    

    However, if you change the above code to the following:

    void func(Derived & obj) {
        obj.foo();  //calls Derived:foo
    }
    

    Now non-const version will be selected, because Base::foo is hidden in Derived class.


    Since Derived::foo hides the Base::foo, so you cannot call the latter using an instance of Derived.

    Now, lets unhide Base::foo and do some more experiments.

    class Derived : public Base {
    public:
        using Base::foo;         //<----------------this unhides Base::foo
        virtual void foo() {
            cout << "Derived::foo()" << endl;
        }
    };
    

    Now in Derived, both functions (const as well as non-const version) are available, unhidden. Now few interesting questions.

    Since now Derived has both functions unhidden, which function will be called in each function below?

    void f(Derived& obj) {
        obj.foo(); //Which function? Base::foo or Derived::foo?
    }
    void g(const Derived & obj) {
        obj.foo(); //Which function? Base::foo or Derived::foo?
    }
    

    First one will call Derived::foo which is non-const version, and second one will call Base::foo which is const version. The reason is simple, with const object, only const functions can be invoked, but with non-const objects, both can be invoked, however non-const version is selected if its available.

    See online demo : http://www.ideone.com/955aY

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

Sidebar

Related Questions

Consider the following program. #include<iostream> using namespace std; class base { public: int _bval;
Consider the following code: class Program { void Foo<T>() { } static void Main(string[]
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following class class test { public: test(int x){ cout<< test \n; } };
consider the following program: namespace NS2 { class base { }; template<typename T> int
Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void
Consider the following code: class Program { static void Main(string[] args) { Department deathStar
Consider the following code: class Program { static void Main(string[] args) { A a
Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);

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.