朋友课程和标题文件

发布于 2025-01-24 20:19:37 字数 1802 浏览 3 评论 0原文

我正在编写一个C ++程序,我希望在其中使用朋友类来访问其他类的受保护成员。 Here is the header for the class I wish to access:

#pragma once

#include <vector>

#include "../glm/glm.hpp"

// An enum class and some structs here

class Polygon {
public:
    // Constructors, destructor...
    Polygon();
    Polygon(int n_vertices, int n_indices);
    Polygon(int n_vertices, int n_indices, float* vertices, unsigned int* indices);
    ~Polygon();
    
    // Loads of other irrelevant functions go here
    
protected:
    // Bunches of other ivars before these
    unsigned int lastVBO;
    unsigned int lastVAO;
    unsigned int lastIBO;
    
    friend class Renderer;
};

And here is the declaration of the class which I wish to be its friend:

#include <string>
#include <iostream>
#include <vector>

#include "../geometry/polygon.hpp"
#include "../geometry/rectangle.hpp"
#include "../geometry/circle.hpp"

class Renderer {
public:
    Renderer();
    ~Renderer();
    void queuePolygon(Polygon* p);
    void drawPolygons();
private:
    // Rectangle and Circle are children of Polygon
    // Which one of these gets called is determined in queuePolygon/drawPolygon
    void queueRectangle(Rectangle* r);
    void queueCircle(Circle* c);
    void drawRectangle(Rectangle* r);
    void drawCircle(Circle* c);
    
    std::vector<Rectangle*>* rectangleQueue;
    std::vector<Circle*>* circleQueue;
};

Now obviously what I want is to get access to the protected members IBO, VBO and VAO from the Polygon class ,为了使用我的渲染器绘制多边形。然而,通过这种实现,即使在多边形课程中的朋友声明中,我也无法访问这些伊瓦尔人。在polygon.hpp中导入渲染器标头会破坏整个内容,因为子类不再表现得很好。

有人以前遇到过这个问题吗?您能告诉我我在做什么错吗?

I am writing a C++ program where I wish to use a friend class in order to access protected members of another class. Here is the header for the class I wish to access:

#pragma once

#include <vector>

#include "../glm/glm.hpp"

// An enum class and some structs here

class Polygon {
public:
    // Constructors, destructor...
    Polygon();
    Polygon(int n_vertices, int n_indices);
    Polygon(int n_vertices, int n_indices, float* vertices, unsigned int* indices);
    ~Polygon();
    
    // Loads of other irrelevant functions go here
    
protected:
    // Bunches of other ivars before these
    unsigned int lastVBO;
    unsigned int lastVAO;
    unsigned int lastIBO;
    
    friend class Renderer;
};

And here is the declaration of the class which I wish to be its friend:

#include <string>
#include <iostream>
#include <vector>

#include "../geometry/polygon.hpp"
#include "../geometry/rectangle.hpp"
#include "../geometry/circle.hpp"

class Renderer {
public:
    Renderer();
    ~Renderer();
    void queuePolygon(Polygon* p);
    void drawPolygons();
private:
    // Rectangle and Circle are children of Polygon
    // Which one of these gets called is determined in queuePolygon/drawPolygon
    void queueRectangle(Rectangle* r);
    void queueCircle(Circle* c);
    void drawRectangle(Rectangle* r);
    void drawCircle(Circle* c);
    
    std::vector<Rectangle*>* rectangleQueue;
    std::vector<Circle*>* circleQueue;
};

Now obviously what I want is to get access to the protected members IBO, VBO and VAO from the Polygon class, in order to draw that polygon using my renderer. Yet with this implementation, I don't have access to those ivars even with the friend declaration in the Polygon class. Importing the renderer header in polygon.hpp breaks the entire thing as subclasses no longer behave nicely.

Has anyone encountered this problem before, and can you tell me what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文