[英]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.