I'm making a small game in the console and have come across a small problem. Let's say I have a class named Canvas
:
canvas.h
class Canvas final {
public:
// Constructor
Canvas(unsigned width = 10, unsigned height = 30);
unsigned outerWidth, outerHeight;
}
canvas.cpp
#include "canvas.h"
Canvas::Canvas(unsigned width, unsigned height) {
outerWidth = width;
outerHeight = height;
// I draw a box (canvas)'s border here
}
Now in the main.cpp
file I declare an instance of the class:
#include "canvas.h"
// The program starts here
int main() {
Canvas myCanvas;
return 0;
}
First of all, I would like to only have 1 member of the Canvas
class, because that's how my program is designed to be. However, when I make an instance of that class, it gets a name (myCanvas
in this example). Then if I want another class (let's say, Entity
) that uses the canvas, I have to say myCanvas.outerWidth
, which is dependent on the object's name. Also, I don't think the myCanvas
variable would be available in the scope of such class anyway.
On the other hand, when I use namespaces, I lose some benefits of using classes (encapsulation (private properties), constructors). So what do I do? Make a static class or namespace or what? I've heard that there isn't a thing called a static class in C++; there are only static properties or methods. I guess putting the static
keyword everywhere isn't good practice. Or is it?