通常,嵌套结构可以访问拥有类public
,protected
和public
成员函数。从嵌套结构中调用基类的protected
成员函数也没有问题,即以下代码编译并正常工作:
#include <iostream>
class Base
{
public:
Base()
{}
protected:
void baseProtectedFunc()
{
std::cout << __func__ << "Called for Base\n";
}
};
class Derived : public Base
{
public:
explicit Derived() : Base()
{}
void accessBaseProtectedFuncFromNested()
{
Nested myNested( this );
myNested();
}
private:
struct Nested
{
explicit Nested( Derived* ptr ) : derived_( ptr )
{}
void operator()()
{
derived_->baseProtectedFunc();
}
Derived* derived_;
};
};
int main( int, char** )
{
Derived myDerived;
myDerived.accessBaseProtectedFuncFromNested();
return 0;
}
现在,请考虑使用mpl::inherit_linearly
使用mpl::vector
类型生成派生基类的以下代码:
#include <iostream>
#include <typeinfo>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/inherit.hpp>
#include <boost/mpl/inherit_linearly.hpp>
#include <boost/mpl/for_each.hpp>
template<typename T>
class Base
{
public:
Base()
{}
protected:
void baseProtectedFunc()
{
std::cout << __func__ << "Called for Base< " << typeid(T).name() << " >\n";
}
};
typedef boost::mpl::vector< long
, unsigned
, bool
, std::string
> parameter_type_list_t;
typedef boost::mpl::inherit_linearly< parameter_type_list_t
, boost::mpl::inherit< boost::mpl::_1
, Base< boost::mpl::_2 > >
>::type base_types;
class Derived : public base_types
{
public:
explicit Derived() : base_types()
{}
template<typename T>
void accessBaseProtectedFuncFromNested()
{
Nested myNested( this );
myNested.someFunc<T>();
}
private:
struct Nested
{
explicit Nested( Derived* ptr ) : derived_( ptr )
{}
template< typename T >
void someFunc()
{
Base<T>* base = static_cast<Base<T>*>( derived_ );
base->baseProtectedFunc();
}
Derived* derived_;
};
};
int main( int, char** )
{
Derived myDerived;
myDerived.accessBaseProtectedFuncFromNested<unsigned>();
return 0;
}
使用GCC版本4.4.6-3(在c ++ 03和c ++ 0x模式下)生成以下错误:
friend-prot.cpp: In member function ‘void Derived::Nested::someFunc() [with T = unsigned int]’:
friend-prot.cpp:47: instantiated from ‘void Derived::accessBaseProtectedFuncFromNested() [with T = unsigned int]’
friend-prot.cpp:82: instantiated from here
friend-prot.cpp:17: error: ‘void Base<T>::baseProtectedFunc() [with T = unsigned int]’ is protected
friend-prot.cpp:72: error: within this context
如果我创建函数,我试图调用public
代码编译并按预期工作。
我可以通过添加一个额外的private
成员函数来解决这个问题,该函数只是转发来自Nested的调用,即:
struct Nested
{
explicit Nested( Derived* ptr ) : derived_( ptr )
{}
template< typename T >
void operator()()
{
derived_->forwarder<T>();
}
Derived* derived_;
};
template< typename T >
void forwarder()
{
Base<T>::baseProtectedFunc();
}
我不明白为什么在baseProtectedFunc()
使用protected
时mpl::inherit
无法拨打{{1}}。
为什么我允许在第一个例子中而不是在第二个例子中调用基类保护函数?
答案 0 :(得分:1)
如果您将转发功能写为
,您会发现会遇到同样的错误template <typename T>
void forwarder()
{
Base<T>* base = static_cast<Base<T>*>( derived_ );
base->baseProtectedFunc();
}
问题是对基础指针的强制转换使编译器难以理解baseProtectedFunc
实际上可以从具有指定指针的当前上下文访问。由于这是模糊的,编译器必须假设不允许通过该指针进行访问。
由于您在转发函数中使用的语法也可以在嵌套类中使用,因此解决方案非常简单和优雅:
struct Nested
{
explicit Nested( Derived* ptr ) : derived_( ptr )
{}
template< typename T >
void someFunc()
{
derived_->Base<T>::baseProtectedFunc(); /* <-- Changed */
}
Derived* derived_;
};