I can come up with several ways to do this.
Create the protected
constructor using only a test subclass.
Add forward the declaration to some dummy class class TestClassThatShouldNotBeUsedInProductionCode;
, then declare a constructor that references this class as a parameter:
A::A( , const TestClassThatShouldNotBeUsedInProductionCode &)
This constructor can simply completely ignore this parameter. Your test module can define this dummy, an empty class: class TestClassThatShouldNotBeUsedInProductionCode {};
and be able to create your class A
using it. Then only your test module can use this constructor, and its name allows you to understand what the purpose of this is. In fact, there is no way to define specific translation units as "real" code compared to "test" code, in C ++, you just want to implement a clear policy that will be difficult to break by accident.
Some options are possible, for example, using an inner class instead of directly declaring an autonomous class. An inner class can only be created with test code.
source share