简体   繁体   中英

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

Hey I have an abstract class named Partition which is a functor, and its a member of my ConcavePolygon class. The Partition Functor depends on a lot of the ConcavePolygon's data such as TPPLPoints and SFMLPoints.

  • I've found that even though i've defined the class inside the one it depends on, i can't easily reach Concaves's data. How do i do this?

  • I also want to use some functions from the Body class, and hoped to do that through ConcavePolygon since its a descendent from it. (needs the AddShape() function);

here's the code if it's helpful:

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

In C++, nested classes are really just a way of scoping namespaces (and providing protection: public/protected/private) for class names. They do not create any special relationship between the two classes besides the name: OuterClass::NestedClass.

So you need to treat nested classes like they are separate classes. If you want the NestedClass to get access to the private members of the OuterClass, you must explicitly declare it a friend of the OuterClass. If you want the NestedClass to access a particular instance of an OuterClass, you must give it an instance of the OuterClass.

Actually this problem has been addressed by a c++ defect report and any current(not too old) c++ compiler should be able handle this:


In 11.7 [class.access.nest] paragraph 1, change

The members of a nested class have no special access to members of an enclosing class, nor to classes or functions that have granted friendship to an enclosing class; the usual access rules (clause 11 [class.access]) shall be obeyed. to

A nested class is a member and as such has the same access rights as any other member.


Here is a working example for you(compiled ok with VS2008, but won't compile with old compiler like 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;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM