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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:37:14+00:00 2026-05-11T16:37:14+00:00

We have a restriction that a class cannot act as a base-class for more

  • 0

We have a restriction that a class cannot act as a base-class for more than 7 classes.
Is there a way to enforce the above rule at compile-time?

I am aware of Andrew Koenig’s Usable_Lock technique to prevent a class from being inherited but it would fail only when we try to instantiate the class. Can this not be done when deriving itself?

The base-class is allowed to know who are its children. So i guess we can declare a combination of friend
classes and encapsulate them to enforce this rule. Suppose we try something like this

class AA {
   friend class BB;
   private:
      AA() {}
      ~AA() {}
};

class BB : public AA {

};

class CC : public AA 
{};

The derivation of class CC would generate a compiler warning abt inaccessible dtor. We can then flag
such warnings as errors using compiler tweaks (like flag all warnings as errors), but i would not like to rely on such techniques.

Another way, but to me looks rather clumsy is:-

class B;

class InheritanceRule{
    class A {
    public:
        A() {}
        ~A() {}
    };
    friend class B;
};

class B {
public:
    class C : public InheritanceRule::A
    {};
};


class D : public InheritanceRule::A{};

The derivation of class D will be flagged as a compiler error, meaning all the classes to be derived should be derived inside class B. This will allow atleast an inspection of the number of classes derived from class A but would not prevent anyone from adding more.

Anyone here who has a way of doing it ? Better still if the base-class need not know who are its children.

NOTE: The class which acts as a base-class can itself be instantiated (it is not abstract).

Thanks in advance,

EDIT-1: As per Comment from jon.h, a slight modification

// create a template class without a body, so all uses of it fail
template < typename D> 
class AllowedInheritance;

class Derived; // forward declaration
// but allow Derived by explicit specialization 
template<> 
class AllowedInheritance< Derived> {};

template<class T>
class Base : private AllowedInheritance<T> {};

// privately inherit Derived from that explicit specialization    
class Derived : public Base<Derived> {};

// Do the same with class Fail Error
// it has no explicit specialization, so it causes a compiler error
class Fail : public Base<Fail> {}; // this is error

int main()
{   
   Derived d;

   return 0;
}
  • 1 1 Answer
  • 1 View
  • 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-11T16:37:15+00:00Added an answer on May 11, 2026 at 4:37 pm

    I’m tired as crap, can barely keep my eyes open, so there’s probably a more elegant way to do this, and I’m certainly not endorsing the bizarre idea that a Base should have at most seven subclasses.

    // create a template class without a body, so all uses of it fail
    template < typename D, typename B> class AllowedInheritance;
    
    
    class Base {};
    class Derived; // forward declaration
    
    // but allow Derived, Base by explicit specialization 
    
    template<> class AllowedInheritance< Derived, Base> {};
    
    // privately inherit Derived from that explicit specialization    
    class Derived : public Base, private AllowedInheritance<Derived, Base> {};
    
    
    // Do the same with class Compiler Error
    // it has no explicit specialization, so it causes a compiler error
    class CompileError: public Base, 
         private AllowedInheritance<CompileError, Base> { };
    
    //error: invalid use of incomplete type 
    //‘struct AllowedInheritance<CompileError, Base>’
    
    
    int main() {
    
       Base b;
       Derived d;
       return 0;
    }
    

    Comment from jon.h:

    How does this stop for instance: class Fail : public Base { }; ? \

    It doesn’t. But then neither did the OP’s original example.

    To the OP: your revision of my answer is pretty much a straight application of Coplien’s “Curiously recurring template pattern”]

    I’d considered that as well, but the problem with that there’s no inheritance relationship between a derived1 : pubic base<derived1> and a derived2 : pubic base<derived2>, because base<derived1> and base<derived2> are two completely unrelated classes.

    If your only concern is inheritance of implementation, this is no problem, but if you want inheritance of interface, your solution breaks that.

    I think there is a way to get both inheritance and a cleaner syntax; as I mentioned I was pretty tired when I wrote my solution. If nothing else, by making RealBase a base class of Base in your example is a quick fix.

    There are probably a number of ways to clean this up. But I want to emphasize that I agree with markh44: even though my solution is cleaner, we’re still cluttering the code in support of a rule that makes little sense. Just because this can be done, doesn’t mean it should be.

    If the base class in question is ten years old and too fragile to be inherited from, the real answer is to fix it.

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

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To summarize the contents of other (already good!) answers, isinstance… May 12, 2026 at 4:20 pm
  • Editorial Team
    Editorial Team added an answer I figured it out: layoutMenus.Show(Cursor.Position.X, Cursor.Position.Y); May 12, 2026 at 4:20 pm
  • Editorial Team
    Editorial Team added an answer I've checked the source of Apple's GCC driver (the one… May 12, 2026 at 4:20 pm

Related Questions

Is it possible to narrow the type of a field in a Java class
I frequently read that struct s should be immutable - aren't they by definition?
I have a legacy database, which I am using EJB3 to model. The database
Suppose you have a subsystem that does some kind of work. It could be

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.