請看下面的例子:如何在C++模板類之間共享受保護的成員?
class _ref
{
public:
_ref() {}
_ref(const _ref& that) {}
virtual ~_ref() = 0;
};
_ref::~_ref() {}
template <typename T>
class ref : public _ref
{
protected:
ref(const _ref& that) {}
public:
ref() {}
ref(const ref<T>& that) {}
virtual ~ref() {}
template <typename U>
ref<U> tryCast()
{
bool valid;
//perform some check to make sure the conversion is valid
if (valid)
return ref<U>(*this); //ref<T> cannot access protected constructor declared in class ref<U>
else
return ref<U>();
}
};
我想所有類型的「裁判」的對象才能夠訪問對方的保護構造。有什麼辦法可以做到這一點?
你試過被'friend'ly? – Yakk 2014-10-01 17:29:43