I know my way around object-oriented programming, but I'm used to Java, and I never touched C++ until recently.
I think my problem is not so much related to syntax as to the philosophy of OOP in C++. I understand the difference between pointers and addresses and the stack and the heap, and stuff, but I still feel like I'm missing something.
Here's an example: I have a class (Shape) that holds some data. I have another class (App) using a number of Shapes.
class Square {
private:
int x;
int y;
int size;
public:
/* constructor */
Square(int x, int y, int size);
}
class App {
private:
Square redSquare;
Square blueSquare;
public:
void setup();
void draw();
}
At some point something is going to instantiate my App and call setup(). The problem is that when I declare the App class (in App.hpp, say) the "redSquare" and "blueSquare" get instantiated, not just declared. Being a Java programmer, I would in this example instantiate my classes in setup(). But thatmeans I can't do it as above, I'll have to set up redSquare and blueSquare as POINTERS, then I can create them using new in setup().
But is that how you would do it? Or would you make a constructor with default parameters, create the redSquare and blueSquare as above, and then set the values of those squares in App.setup(), using something like a Square.init(x, y, size) or something? Or some other way?
Do you ever aggregate classes, or only pointers?
I can certainly hack this one way or the other so it works myself, but I have a feeling I'm doing things "the Java way" and not getting how C++ programmers think.