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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:45:49+00:00 2026-05-24T21:45:49+00:00

I have a base class called Object. PhysicsObject inherits from Object. Ball inherits from

  • 0

I have a base class called Object. PhysicsObject inherits from Object. Ball inherits from PhysicsObject, and SoftBall inherits from Ball. Something like this:

Object
 |
PhysicsObject
 |
Ball
 |
SoftBall

I have a method called foo() that is declared virtual in Object (given an implementation, so not pure virtual), then declared and implemented as virtual again in PhysicsObject and Ball. Finally, SoftBall implements foo() again without declaring it as virtual.

If I have an Object* that points to a SoftBall, will SoftBall’s version of foo() be called? If not, is there any way to achieve this effect? Basically, does this aspect of polymorphism still work over more than one level of inheritance?

  • 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-24T21:45:49+00:00Added an answer on May 24, 2026 at 9:45 pm

    If I have an Object* that points to a SoftBall, will SoftBall’s version of foo() be called? If not, is there any way to achieve this effect? Basically, does this aspect of polymorphism still work over more than one level of inheritance?

    Yes

    C++ defines methods as virtual if the corresponding overload is virtual in the base class (unlike other OO languages, where overrides have to be marked explicitely).

    http://codepad.org/pL09QWNN


    Note that with some more ingredients you can get really funky:

    #include <iostream>
    
    struct A { virtual void foo() { std::cout << "A::foo();" << std::endl; } };
    struct B { virtual void foo() { std::cout << "B::foo();" << std::endl; } };
    
    struct Oa: A, B { using A::foo; };
    struct Ob: A, B { using B::foo; };
    
    #define TEST(a) std::cout << #a << ":\t"; a
    
    int main()
    {
        A a;
        B b;
        Oa oa;
        Ob ob;
    
        TEST(a.foo());
        TEST(b.foo());
        TEST(oa.foo());
        TEST(ob.foo());
    
        std::cout << "But oa through different references:" << std::endl;
        {
            A& ar = oa;
            TEST(ar.foo());
    
            B& br = oa;
            TEST(br.foo());
        }
    
        std::cout << "And ob through different references:" << std::endl;
        {
            A& ar = ob;
            TEST(ar.foo());
    
            B& br = ob;
            TEST(br.foo());
        }
    
        return 0;
    }
    

    Try to predict what gets printed here. It get’s a lot more fun once you mixin:

    • (mutable/const/volatile qualified) overloads
    • (conflicting) default arguments

    I remember reading quite a lot of more and less horrendous examples in the form of trivia question I hope to never encounter in an interview. Although I know what I’d answer: “If you have code like this, I’m not sure I want the job”?.

    You could go looking on Herb Sutter, Scott Meyer, and you’ll be amazed what pitfalls lurk in our otherwise-so-civilized-nice-little-language-fondly-referred-to-as C++

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

Sidebar

Related Questions

I have a base class called Base . This base class has a method
I have a Base Class, called primitive Graphics. derived from this class are several
I have an abstract base class called Shape from which both Circle and Rectangle
I have a base class that I want to look like this: class B
I have created a class called BasePage which inherits System.Web.UI.Page. On this page I've
I have a base class called Geometry from which there exists a subclass Sphere
I have one base class which holds a map for function pointers like this
I have a base class called Component. I also have 2 interfaces I2DComponent, and
Ok so I have an abstract base class called Product, a KitItem class that
I have a class Contact (base class),a class called Customer and a class called

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.