如何从类的一个成员类/结构内部访问类的成员数据

How to reach the member data of a class from inside one of its member class/struct?

本文关键字:结构 内部 访问 数据 成员类 成员 一个      更新时间:2023-10-16

嘿,我有一个抽象类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;
};