In C++, can I prevent derived class being instantiated by classes other than friends

262 Views Asked by At

In C++, if I have a abstract base class, is it possible to prevent its derived classes being instantiated by classes other than friends that the base class knows?

2

There are 2 best solutions below

0
On

You can define constructors as private, just like any other function. For example:

class foo
{

friend foo *FooConstructor(void);

public:
  void Method();
  void Method2();

private:
  foo();
  foo(const &foo);
};

foo *FooConstructor(void) 
{ 
  return new foo(); 
}

This prevents a foo being created in any way, save with the FooContructor function.

0
On

There are two ways you can have a base-class internal

The first is to make the constructors private, like this:

struct Sub1;
struct Sub2;

struct Base
{
    virtual ~Base() = default;
private:
    Base() = default;
    Base(const Base&) = default;
    friend struct Sub1;
    friend struct Sub2;
};

struct Sub1 : protected Base {}; // ok, its a friend
struct Sub2 : protected Base {}; // ok, its a friend
struct Sub3 : protected Base {}; // compiler error

The second way is to declare the base-class in a anonymous namespace:

namespace {
    struct Base{};
}

struct Sub : Base {}; 

Now, all classes in the same translation unit can use Base, but other classes wont know it exists.

This way is often less desirable, since derived classes can only be used as incomplete types (forwarded)