【问题标题】:How to reach the member data of a class from inside one of its member class/struct?如何从其成员类/结构之一中获取类的成员数据?
【发布时间】:2011-07-15 22:37:59
【问题描述】:

嘿,我有一个名为 Partition 的抽象类,它是一个函子,它是我的 ConcavePolygon 的成员> 类。 Partition Functor 依赖于很多 ConcavePolygon 的数据,例如 TPPLPoints 和 SFMLPoints。

  • 我发现即使我已经在它里面定义了类 取决于,我无法轻易获得 Concaves 的数据。 我该怎么做 这个?

  • 我也想使用 Body 类中的一些功能,并希望 通过 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

【问题讨论】:

  • 提示:不要过度使用 cmets。 ////////// 的大行字根本不会提高阅读效果,反而会降低舒适度。此外,代码告诉如何,如果你做得对,它会告诉你什么,而 cmets 应该告诉你为什么。所以说//// Constructors //// 是完全没有必要的,代码已经说了。 Run the Partitioning Algorithm 也是如此。不过//clear everything to be filled with the Partition Algorithm 没问题,因为它解释了为什么会发生某些事情。

标签: c++ class polymorphism membership functor


【解决方案1】:

在 C++ 中,嵌套类实际上只是为类名确定命名空间(并提供保护:公共/受保护/私有)的一种方式。除了名称之外,它们不会在两个类之间创建任何特殊关系:OuterClass::NestedClass。

因此,您需要将嵌套类视为单独的类。如果您希望 NestedClass 能够访问 OuterClass 的私有成员,则必须显式声明它为 OuterClass 的朋友。如果您希望 NestedClass 访问 OuterClass 的特定实例,您必须它一个 OuterClass 的实例。

【讨论】:

    【解决方案2】:

    实际上这个问题已经被 c++ 缺陷报告解决了,任何当前(不是太旧)的 c++ 编译器都应该能够处理这个问题:


    在 11.7 [class.access.nest] 第 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;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      相关资源
      最近更新 更多