Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
class UiControl { protected: UiControl() { //The constructor UiControl is private, only objects of subclasses can be created } public: int x; int y; int width; int height; enum TypeTag { BUTTON, TEXT_FIELD }; TypeTag typeTag; virtual void draw() { } }; class Button : public UiControl { public: string caption; ofColor backgroundColor; ofColor foregroundColor; Button(int x, int y, int width = 200, int height = 50, string caption = "") { this->x = x; this->y = y; this->width = width; this->height = height; this->caption = caption; this->backgroundColor = blue; this->foregroundColor = grey; this->typeTag = BUTTON; } void draw() { ofSetColor(this->backgroundColor); ofRect(this->x, this->y, this->width, this->height); } }; class UiTools { public: std::vector<UiControl*> uiControls; UiTools(int screenWidth, int screenHeight) { windowResized(screenWidth, screenHeight); Button* button = new Button(10, 10); (*button).caption = "Text"; uiControls.insert(uiControls.begin(), button); } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Basis { private: int m_x; public: Basis(int x) : m_x(x) { } }; class Child : public Basis { public: Child(int x) : Basis(x) { } }; |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Oder auch nicht...und eine get-Methode anbieten
Oder auch nicht...und eine get-Methode anbieten
Verwendest du Initialisierungslisten, um die Laufzeit zu reduzieren, damit die Attribute direkt mit dem richtigen Wert initialisiert werden und nicht weil du es so lesbarer findest?
Nur dann finde ich es komisch, für Attribute wie x und y Koordinaten Setter und evtl. Getter einzubauen.
So muss doch noch ein Element mehr auf dem Stack gelegt werden, was das Program wieder langsamer macht.
Zitat von »MitgliedXYZ«
So muss doch noch ein Element mehr auf dem Stack gelegt werden, was das Program wieder langsamer macht.
Was genau meinst du?
C-/C++-Quelltext |
|
1 |
Button* button = new Button(10, 10, 200, 50); |
C-/C++-Quelltext |
|
1 2 3 |
Position position = { 10, 10 }; Size size = { 200, 50 }; Button* button = new Button(position, size); |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
struct Position { int x; int y; }; struct Size { int width; int height; }; class UiControl { protected: Position position; Size size; UiControl(Position position, Size size) { this->position = position; this->size = size; } void setPosition(Position position) { this->position = position; } Position getPosition() { return position; } void setSize(Size size) { this->size = size; } Size getSize() { return size; } public: virtual void draw(); }; class Button : public UiControl { public: string caption; ofColor backgroundColor; ofColor foregroundColor; Button(Position position, Size size, string caption = "") : UiControl(position, size) { this->caption = caption; this->backgroundColor = blue; this->foregroundColor = grey; } void draw() { ofSetColor(this->backgroundColor); ofRect(position.x, position.y, size.width, size.height); } }; |
Also ich meinte, dass button.x = 5; eine kürzere Ausführungszeit benötigt, als button.setPosX (5); da so eine Methode auf dem Call Stack abgelegt wird und erst danach die Variable verändert wird. Kann natürlich sein, dass der Geschwindigkeits Unterschied gering ist, bzw. vom Compiler so optimiert wird, dass die Methode in eine Inline-Methode umgewandelt wird, also beide Varianten den selben Code nach dem Compilieren produzieren.
Also ich meinte, dass button.x = 5; eine kürzere Ausführungszeit benötigt, als button.setPosX (5); da so eine Methode auf dem Call Stack abgelegt wird und erst danach die Variable verändert wird. Kann natürlich sein, dass der Geschwindigkeits Unterschied gering ist, bzw. vom Compiler so optimiert wird, dass die Methode in eine Inline-Methode umgewandelt wird, also beide Varianten den selben Code nach dem Compilieren produzieren.
Den Code habe ich mal verbessert. Mit den Struct für die Position und Größe wird die Benutzung aber umständlicher.
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
struct Position { int x; int y; Position(int _x = 0, int _y = 0) : x(_x), y(_y) { } }; struct Size { int width; int height; Size(int _width = 0, int _height = 0) : width(_width), height(_height) { } }; Button* button = new Button(Position(10, 10), Size(200, 50)); |
Werbeanzeige