Note that there are some explanatory texts on larger screens.

plurals
  1. POJava programmer - how do C++ people use classes? Pointers to classes, default parameters?
    primarykey
    data
    text
    <p>I know my way around object-oriented programming, but I'm used to Java, and I never touched C++ until recently. </p> <p>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.</p> <p>Here's an example: I have a class (Shape) that holds some data. I have another class (App) using a number of Shapes. </p> <pre><code>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(); } </code></pre> <p>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(). </p> <p>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? </p> <p>Do you ever aggregate classes, or only pointers?</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload