C++ 如何访问嵌套类的私有成员?
A.hppC++ 如何访问嵌套类的私有成员?,c++,C++,A.hpp class A { public: class B { int x; public: B(int f); }; void alpha(B *random); }; void A::alpha(A::B *random) { // access x here, how to do it? } class A { public: class B { int x; public: B(int f); }
class A
{
public:
class B
{
int x;
public:
B(int f);
};
void alpha(B *random);
};
void A::alpha(A::B *random)
{
// access x here, how to do it?
}
class A
{
public:
class B
{
int x;
public:
B(int f);
};
virtual void alpha(B *random) = 0;
};
class C : public A
{
public:
virtual void alpha(B *random);
};
void C::alpha(A:B *random)
{
// access x here, how to do it?
}
A.cpp
class A
{
public:
class B
{
int x;
public:
B(int f);
};
void alpha(B *random);
};
void A::alpha(A::B *random)
{
// access x here, how to do it?
}
class A
{
public:
class B
{
int x;
public:
B(int f);
};
virtual void alpha(B *random) = 0;
};
class C : public A
{
public:
virtual void alpha(B *random);
};
void C::alpha(A:B *random)
{
// access x here, how to do it?
}
私有变量正在某个地方设置,我想在这个alpha
函数中访问该值。如何访问alpha()
内部的x
编辑:第二个问题:
A.hpp
class A
{
public:
class B
{
int x;
public:
B(int f);
};
void alpha(B *random);
};
void A::alpha(A::B *random)
{
// access x here, how to do it?
}
class A
{
public:
class B
{
int x;
public:
B(int f);
};
virtual void alpha(B *random) = 0;
};
class C : public A
{
public:
virtual void alpha(B *random);
};
void C::alpha(A:B *random)
{
// access x here, how to do it?
}
C.hpp
class A
{
public:
class B
{
int x;
public:
B(int f);
};
void alpha(B *random);
};
void A::alpha(A::B *random)
{
// access x here, how to do it?
}
class A
{
public:
class B
{
int x;
public:
B(int f);
};
virtual void alpha(B *random) = 0;
};
class C : public A
{
public:
virtual void alpha(B *random);
};
void C::alpha(A:B *random)
{
// access x here, how to do it?
}
C.cpp
class A
{
public:
class B
{
int x;
public:
B(int f);
};
void alpha(B *random);
};
void A::alpha(A::B *random)
{
// access x here, how to do it?
}
class A
{
public:
class B
{
int x;
public:
B(int f);
};
virtual void alpha(B *random) = 0;
};
class C : public A
{
public:
virtual void alpha(B *random);
};
void C::alpha(A:B *random)
{
// access x here, how to do it?
}
您可以使类
A
成为类B
的“朋友”,从而允许A
访问B
的私人成员
合并文件以便于编译:
A类
{
公众:
B类
{
A类朋友;//***这里***
int x;
公众:
B(int f);;
};
空隙α(B*随机);
};
void A::alpha(B*随机)
{
int x=随机->x;
}
int main(){}
如果一个类是另一个类的朋友,它可以访问另一个类的私有成员,如本例所示:
class A {
public:
class B {
friend class A;
int x;
public:
B(int f);
};
void alpha(B *random) { random->x = 10; }
};
如果B
是a
的私有嵌套类,则可以将其成员公开。他们可以访问A
而不是其他任何东西。对不起,这是个错误。B类是公共的,但其中的“int x”是私有的@NathanPierson感谢您的回复。我有一个类似的第二个问题。我在问题中添加了它作为编辑。那么我需要在C.hpp或A.hpp中添加朋友吗?@galaxy2096-您需要编辑的头文件是包含B
定义的头文件(在您的例子中是A.hpp)。具体来说,类B
需要将需要访问B
s私有成员的所有其他类声明为朋友。因此,如果A
的成员需要访问权限(例如A::alpha()
和A
的其他成员),则A
需要成为朋友
而C
的成员需要访问权限,则C
需要成为朋友。类的友谊不是继承的-将A
声明为friend
不会导致C
成为friend
,相反,将C
声明为friend
不会导致A
成为friend
更改C::alpha()
的声明,从void alpha(B*)
至无效alpha(A::B*)
。或者,使用C++11及更高版本,在C
的定义中使用override
(即,执行void alpha(B*)override
将导致编译器告诉您C::alpha()
不是覆盖A::alpha()
,而是覆盖void alpha(A::B*)重写
会起作用。@Peter是的,我以前就有过。我忘了在问题中添加它。但是当我试图在“friend class A”下面添加“friend class C”时,它抛出一个错误,说它不知道class C,然后将class C
的声明放在任何类之外)在定义B
之前,或将好友声明更改为friend class::C
(或两者兼有)。您需要记住,您要声明/定义的每个类都在一个范围内,并且任何声明(例如作为好友)或用法都需要正确(明确地)定义合格。如果编译器认为它位于与您不同的作用域中,则编译器获胜。