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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:37:34+00:00 2026-05-24T01:37:34+00:00

So let us say I have some chain of classes where each class is

  • 0

So let us say I have some chain of classes where each class is derived from the class before it. For whatever reason, they all like to use the same name for some member function. Kinda like this:

class C1 { public:             void f() { cout<<"C1"; }; };
class C2 : public C1 { public: void f() { cout<<"C2"; }; };
class C3 : public C2 { public: void f() { cout<<"C3"; }; };

Obviously, if I just declare some objects, then call the function f from them, all will call the function associated with their respective object-type:

C1 c1; c1.f(); // prints C1
C2 c2; c2.f(); // prints C2
C3 c3; c3.f(); // prints C3

Now, if I declare some pointers-to-objects, then call the function f from them, all will call the function associated with their respective pointer-type:

C1* p1 = &c1; p1->f(); // prints C1
C1* p2 = &c2; p2->f(); // prints C1
C1* p3 = &c3; p3->f(); // prints C1
C2* p4 = &c2; p4->f(); // prints C2
C2* p5 = &c3; p5->f(); // prints C2
C3* p6 = &c3; p6->f(); // prints C3

All of this is super. I either call the function associated with the object’s type or I call the function associated with the pointer’s type…

Or of course I could make the function ‘virtual’. Then if I call the function from some object, I will get no change in behavior; however, if I call the function from some pointer, then I won’t just call the function for the pointer’s type, I will actually call the function for the object-type that the pointer is pointing to. So far so good.

I can even make the change to virtual mid-way through the chain of inheritance. Let’s say I put a virtual before the function f inside of class C2. Now the function has been made virtual (i.e. when called from pointers, it uses the object-pointed-to-type instead of the pointer-type to resolve the function call), not only for its own class, but for all future classes that are derived from it.

My question is this: Once a function has been declared virtual (at some point in the chain of inheritance) can it ever be reverted back to being non-virtual (further down the chain of inheritance)?

For clarification: When I say revert back to non-virtual behavior, I mean that when I call the function from a pointer, it will use the pointer’s type to resolve the function call (and not the object-type that the pointer is pointing to).

  • 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-24T01:37:34+00:00Added an answer on May 24, 2026 at 1:37 am

    Once a function has been declared virtual (at some point in the chain of inheritance) can it ever be reverted back to being non-virtual (further down the chain of inheritance)?

    No. Once a function signature has been made virtual somewhere along the line of inheritance, it will stay that way: every function with the same signature in a derived class will be virtual as well.

    One way to get around this is to use (abuse?) the template method pattern:

    struct Base {
        virtual void doFoo() { bar(); }
        void foo() { doFoo(); }
    };
    
    struct Derived1 : public Base {
        virtual void doFoo() { baz(); }  // "overrides" foo via doFoo
    };
    
    struct Derived2 : public Base {
        void foo() { quux(); }  // "un-virtualize" foo by decoupling it from doFoo
    };
    

    In this inheritance tree, the pointer type will determine which foo is called; if it’s the one from Base, the pointed-to object’s type will determine which doFoo is called.

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

Sidebar

Related Questions

Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
Let's say I have some classes like: <div class=something style=display: none>Some text I would
Let's say I have some class X that is going to be injected all
Let's say I have some model objects that resemble this: public class FooModel {
Let's say I have some templated class depending on type T . T could
Let's say I have the following. <div class=foo> <div> some text <div class=bar> </div>
Let's say we have some block: <div class=block></div> There are three sizes of block
Let's say we have some Foo class that contains public void setOnSomethingListener(OnSomethingListener listener); We
Let's say I have some Django model that is an abstract base class: class
Let's say I have some hyperlink but class and id of it are already

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.