Search code examples
c++unit-testingusing

using keyword for make public a protected overloaded method


I tried to search solution for this problem, but I could not find any. This is my class:

class X;
class MyClass
{
  public:
    MyClass();

  protected:
    // ctor for unit test
    MyClass(std::shared_ptr<X> p_x);
};

In unit test:

class FakeClass : public MyClass
{
  public:
    using MyClass::MyClass;
};

In the tests I want to use it:

FakeClass myFake(std::shared_ptr<X>(new X));

But g++ says:

MyClass::MyClass(std::shared_ptr) is protected

How can be specified the exact method for using?


Solution

  • FakeClass can use the MyClass constructor, but wherever you're constructing your FakeClass from can't - it's not a friend or a derived type.

    You'll have to write a public FakeClass constructor, and that has to be what's calling the MyClass protected constructor:

    FakeClass(std::shared_ptr<X> p)
    : MyClass(p)
    { }