朋友课程和标题文件
我正在编写一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论