Question 29: The C++ code below generates a compiler error. Which of the following solutions can be used to correctly access the member named nested?
class SomeClass
{
public:
int data;
protected:
class Nest
{
public:
int nested;
};
public:
static Nest* createNest(){return new Nest;}
};
修改后:
class SomeClass
{public:
int data;
public:
class Nest
{
// friend void use_someclass();
public:
int nested;
};
public:
static Nest* createNest(){return new Nest;}
};
class d:SomeClass{};
void use_someclass()
{
SomeClass::Nest* nst = SomeClass::createNest();
//d * nst = SomeClass::createNest();;
//nst
nst->nested = 5;
}
class Nest 为 public:这样 SomeClass::Nest* nst 中,Nest 就能成功访问。 SomeClass::Nest* nst = SomeClass::createNest();后边的createNest() 不需要public 权限。其实 protected 也可以,but 前面的Nest *nst指针就不行。