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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:47:29+00:00 2026-05-17T18:47:29+00:00

I would to block child classes from overriding a base method and have the

  • 0

I would to block child classes from overriding a base method and have the child classes override a new method in a parental class. In other words, a child class of the base class blocks the base class methods and delegates to a new method that further child classes must override. I still want the base class method to be available.

Here is an example:

#include <iostream>
#include <string>

struct Base
{
    virtual const std::string&  class_name(void) = 0;
};

struct Level1
    : public Base
{
private:  // Prevent child classes from overriding
          //     the Base::class_name method 
    const std::string& class_name(void)
        {
            static std::string name;
            name = "class" + class_name_from_level_1();
            return name;
        }
protected:
    // This is the "new" or redirected class that child classes
    //    must override.
    virtual const std::string& class_name_from_level_1(void) = 0;
};

struct Level2
    : public Level1
{
    static std::string  name;

    const std::string&  class_name_from_level_1(void)
        {
            if (name.length() == 0)
            {
                name = "Level2";
            }
            return name;
        }
};


int main(void)
{
    Level2  lev2;
    std::cout << lev2.class_name() << "\n";
    return 0;
}

I am getting the following errors from g++:

$ g++ hiding_virt_methods.cpp -o hiding_virt_methods.exe
hiding_virt_methods.cpp: In function `int main()':
hiding_virt_methods.cpp:15: error: `virtual const std::string& Level1::class_name()' is private
hiding_virt_methods.cpp:43: error: within this context

In the above example, I want the following chain of execution for Level2:
Base::class_name() –> Level1::class_name_from_level_1() –> Level2::class_name_from_level_1()

Also, I only want to block inheritance of specific methods in the Base class. Protected and Private Inheritance affect all the public methods.

So how do I stop the chain of inheritance of specific Base methods at different levels in the inheritance tree?

Edit: Real world example.
I have an interface class Record. Class Record_With_Id inherits from class Record and adds an ID field. The class Record contains an accept_visitor method. Class Record_With_Id overrides accept_visitor to apply to the ID field, then calls a virtual method, record_with_id_accept_visitor, which descendants must implement.

  • 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-17T18:47:29+00:00Added an answer on May 17, 2026 at 6:47 pm

    For your immediate problem, you can rename your class_name() functions to class_name_impl() or similar, then in the base class have a class_name() function that calls the implementation one. That way, only the base class version will match when calling class_name() on a derived object.

    More generally, you can frustrate attempts to call the base class methods by having same-named functions in the derived classes – as you’ve done, but anyone can cast to a Base& and call whatever they like. You can’t stop virtual methods being overridable in derived classes… you can only frustrate their use.

    It’s worth remembering that a publicly derived class IS an instance of the base class, and SHOULD provide the base class’s interface.

    EDIT: re yout “real world example” edit, can you explain the problem with a normal implementation ala…

    #include <iostream>
    
    struct Visitor
    {
        virtual void operator()(int&) const = 0;
    };
    
    struct X
    {
        virtual void visit(Visitor& v) { v(a); v(b); }
        int a;
        int b;
    };
    
    struct X_with_C : X
    {
        int c;
        virtual void visit(Visitor& v) { X::visit(v); v(c); }
    };
    
    struct My_Visitor : Visitor
    {
        void operator()(int& n) const { std::cout << ++n << '\n'; }
    };
    
    int main()
    {
        X x;
        x.a = 10;
        x.b = 20;
        My_Visitor visitor;
        x.visit(visitor);
        X_with_C xc;
        xc.a = -10;
        xc.b = -20;
        xc.c = -30;
        xc.visit(visitor);
        X& rx = xc;
        rx.visit(visitor);
    }
    

    Output:

    11
    21
    -9
    -19
    -29
    -8
    -18
    -28
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why you would want to use a switch block over a series of if
I'm looking for a Python module that would take an arbitrary block of text,
Would the following SQL remove also the index - or does it have to
I want to inherit from a class which is located in a WCF Service.
Given a DIV with this general structure (class=post, from an extracted message board post)
From what I understand about twisted, nothing running in the reactor thread should block.
Would NTFS allocation blocks of 16KB or 32KB make compile time faster in comparison
How would you store formatted blocks of text (line breaks, tabs, lists - etc.)
In the Oracle world, it's been gospel to build your database block size to
Would it not make sense to support a set of languages (Java, Python, Ruby,

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.