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?