[英]How to reach the member data of a class from inside one of its member class/struct?
嘿,我有一个名为Partition的抽象 class ,它是一个函子,它是我的ConcavePolygon class 的成员。 Partition Functor 依赖于很多 ConcavePolygon 的数据,例如 TPPLPoints 和 SFMLPoints。
我发现即使我已经在它所依赖的那个内部定义了 class,我也无法轻易获得 Concaves 的数据。 我该怎么做呢?
我还想使用Body class 中的一些功能,并希望通过 ConcavePolygon 来做到这一点,因为它是它的后代。 (需要 AddShape() 函数);
如果有帮助,这是代码:
class ConcavePolygon : public Body{
protected:
std::list<Vector2f> SFMLPoints;
std::vector <TPPLPoint> TPPLPoints; //TODO: figure out how to make a temp version without Memory Exception
public:
//////////////////// Partitioning/Triangulating Classes /////////////////////////////////////////////////////////////
class Partition{
protected:
virtual void RunAlgorithm(){};
public:
Partition(Vector2f* Points, long numbPoints){ //TODO turn this into a base class for triangulate or Convexulate
//rev up all the needed data structs
std::list<TPPLPoly> PartitionOutput;
std::list <TPPLPoly> ::iterator I;
//Backup the points, and convert them to tppl
for(int I=0; I<numbPoints; I++){
TPPLPoints.push_back(TPPLPoint(Points[I].x, Points[I].y));
SFMLPoints.push_back(Points[I]);}
TPPLPoly Poly(&TPPLPoints[0], numbPoints, false);
//clear everything to be filled with the Partition Algorithm
this->Clear();
// Run the Partitioning Algorithm
RunAlgorithm();
// Convert results to SFML points, shapes, and add to the body
for( I= PartitionOutput.begin(); I!= PartitionOutput.end();I++){
sf::Shape TempShape;
for(int i=0; i< I->GetNumPoints(); i++)
TempShape.AddPoint( I->GetPoint(i).x, I->GetPoint(i).y);
this->AddShape(TempShape);
}
};
};
class Convexulate: public Partition{
bool RunAlgorithm(TPPLPoly& Poly, std::list<TPPLPoly>& PartitionOutput){
TPPLPartition Partition;
Partition.ConvexPartition_OPT(&Poly, &PartitionOutput);
};
};
class Triangulate: public Partition{
bool RunAlgorithm(TPPLPoly& Poly, std::list<TPPLPoly>& PartitionOutput){
TPPLPartition Partition;
Partition.Triangulate_OPT(&Poly, &PartitionOutput);
};
};
//////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////// Constructors /////////////////////////////////////////////////////
ConcavePolygon(Vector2f* Points, long numbPoints){
Convexulate(Points, numbPoints);
};
};// ConcavePolygon Class
在 C++ 中,嵌套类实际上只是为 class 名称确定命名空间范围(并提供保护:公共/保护/私有)的一种方式。 除了名称之外,它们不会在两个类之间创建任何特殊关系:OuterClass::NestedClass。
因此,您需要将嵌套类视为单独的类。 如果您希望 NestedClass 能够访问 OuterClass 的私有成员,则必须显式声明它为 OuterClass 的朋友。 如果您希望 NestedClass 访问 OuterClass 的特定实例,则必须为其提供OuterClass 的实例。
实际上,c++ 缺陷报告已经解决了这个问题,任何当前(不太旧)的 c++ 编译器都应该能够处理这个问题:
在 11.7 [class.access.nest] 第 1 段中,更改
嵌套 class 的成员对封闭 class 的成员没有特殊的访问权限,也对与封闭 class 授予友谊的类或函数没有特殊访问权限; 应遵守通常的访问规则(第 11 条 [class.access])。 至
嵌套的 class 是一个成员,因此具有与任何其他成员相同的访问权限。
这是一个适合您的工作示例(使用 VS2008 编译正常,但不会使用 VC6 等旧编译器编译):
class Body{
public:
void foo()
{
}
};
class Outer: public Body {
public:
class Inner{
public:
Inner(Outer& out):m_rOut(out){}
void foo()
{
m_rOut.m_out = 1; //access private member of Outer class directly
m_rOut.foo(); //call member function of Body
}
private:
int m_inner;
Outer& m_rOut;
};
private:
int m_out;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.