如何从类的一个成员类/结构内部访问类的成员数据
How to reach the member data of a class from inside one of its member class/struct?
嘿,我有一个抽象类Partition,它是一个函子,它是我的ConcavePolygon类的成员。分割函数依赖于许多凹多边形的数据,如tplpoints和SFMLPoints。
-
我发现,即使我已经定义了类里面的一个它取决于,我不能轻易拿到Concaves的数据。我怎么做这个吗?
-
我还想使用一些来自Body类的函数,并希望通过conavepolygon来实现,因为它是它的后代。(需要
如果有帮助的话,下面是代码:
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++中,嵌套类实际上只是为类名限定命名空间范围(并提供保护:public/protected/private)的一种方式。除了名称外,它们不会在两个类之间创建任何特殊关系:OuterClass::NestedClass。
所以你需要像对待单独的类一样对待嵌套类。如果您希望NestedClass能够访问OuterClass的私有成员,您必须显式地将其声明为OuterClass的友类。如果你想让NestedClass访问OuterClass的一个特定实例,你必须给它一个OuterClass的实例。
实际上这个问题已经被c++缺陷报告解决了,任何当前的(不太旧的)c++编译器都应该能够处理这个问题:
In 11.7 [class.access. net]第1段,修改
嵌套类的成员不能访问封闭类的成员,也不能访问已授予封闭类友好关系的类或函数;应遵守通常的访问规则(第11条[class.access])。
嵌套类是一个成员,因此具有与任何其他成员相同的访问权限。
这里是一个工作的例子(编译好的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;
};
相关文章:
- C++ 结构内部的unordered_map会导致内存泄漏问题吗?
- 在C++中更改结构内部元素的方法?
- 结构内部结构的联合 - 混合 C 和 C++
- 如果在结构内部使用condition_variable,则它不起作用
- 尝试将数据添加到结构内部的矢量时出错
- 如何使结构内部的可变参数模板可以在外部访问?
- 结构内部的结构:"对非静态成员的非法引用"错误
- C 是什么是在结构内部洗牌的最有效方法
- C :结构内部的Armadillo矩阵
- 使用包含该结构内部结构的 using 定义
- 初始化指向另一个结构内部结构的指针
- 更改结构C 内部阵列的大小
- 如何与CUDA中的结构内部结构一起工作
- 如何在C++中访问数组结构内部的数组结构
- 另一个结构内部的结构数组的成员变量没有通过引用传递
- 结构内部的C++数组,如何处理它们
- 从构造函数中本地声明的device_vector使用device_vector::data()方法初始化结构内部的指针是
- 使用constexpr方法在结构内部进行模板参数化
- 访问结构内部的矢量size_type
- CPP 结构内部重载 () 运算符