【发布时间】:2016-10-17 06:44:19
【问题描述】:
我在 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
}
现在我必须直接使用“keyToNextLvl”方法来访问它。 (我不允许访问accept方法-ques本身提供了使用accept方法访问keyToNextLvl的示例解决方案,所以我需要提供AnswerKey的实现)
我进行了一些搜索,并获得了一些无需使用朋友即可访问私人成员/方法的方法 http://bloglitb.blogspot.in/2010/07/access-to-private-members-thats-easy.html
但我对上述问题的解决方案没有任何想法。
【问题讨论】:
标签: c++ inner-classes private-members