Sat*_*nis 3 c++ inner-classes private-members
我在CPP测验中发现了一个问题.问题是
class secret
{
class hidden{};
public:
template <class K>
string accept(K k) {return (k(*this, hidden()));}
string keyToNextLvl (hidden )const {return ("success!"); }
};
struct SampleSoln
{
template <class pwd>
string operator()(const secret &sc, pwd opwd) const
{ return (sc.keyToNextLvl(opwd)); }
};
int main()
{
secret sc;
cout <<sc.accept(SampleSoln()) << endl; //Prints success
cout <<sc.keyToNextLvl (AnswerKey()) << endl; //Need to provide the implementation of AnswerKey
}
Run Code Online (Sandbox Code Playgroud)
现在我必须直接使用方法"keyToNextLvl"访问它.(我不允许访问accept方法 - 在问题本身中提供了样本解决方案,用于使用accept方法访问keyToNextLvl.所以我需要提供AnswerKey的实现)
我做了一些搜索并找到了一些方法来访问私人成员/方法而不使用朋友 http://bloglitb.blogspot.in/2010/07/access-to-private-members-thats-easy.html
但我对上述问题的解决方案一无所知.
得到它了!
struct AnswerKey
{
template <class T>
operator T ()
{
return T();
}
};
Run Code Online (Sandbox Code Playgroud)
使用模板化转换运算符构造secret::hidden对象