C/C++:继承基类模版之后的友元声明

       这段代码中如果不将继承的基类模版声明为友元类就会出现编译错误,因为基类函数调用了子类的保护(也可以是私有)构造函数

template 
class Base
{
public:
    static T* instance()
    {
        return new T();
    }

    Base() = default;
    virtual ~Base() = default;
};

class Derived : public Base
{
    friend class Base;

protected:
    Derived() = default;
};

int main()
{
    Derived* derive = Derived::instance();
}

 

你可能感兴趣的:(C/C++)