c++ - How to Initialize an array of my object in another class -
I am creating a game in which my main class "inGame" is to control the game objects. There will be many other custom made classes inside "InGame".
Like,
square mouse {int x, y; Bull Survive; Speed of float; Public: mouse (int xx, int yy, float spd, bool alv) {x = xx; Y = yy; Speed = spd; Alive = alv; } // member work}; Class inGame {mouse m1; Dog D1; // Single Object which is easy to create public: inGame (): D1 (200, 400, 1.5, false), M1 (40, 40, 0.5, false) // It works fine {} // member work};
But now I can say 3 moses because I thought of M1 [3] or maybe a vector.
class inGame {mouse m1 [3]; // I want 3 mouse (animals) in the game Mouse Dog D1; // Single object that is easy to build public: inGame (): D1 (200, 400, 1.5, wrong) // Confusion here; M1 (40, 40, 0.5, false), {} member};
So I have tried the following:
inGame (): M1 [0] (1,2,3, true), M1 [1] (2,3,4, true), M1 [2] (3,4,5, true) {} / / in in the game No: M1 ((1,2,3, true) There is no game in (2,3, 4, true), (3, 4, 5, true)} {}: M1 ((1,2,3, true), (2,3,4, True), (3,4, 5, true);} {}
Even if I use std :: vector m1 then i through inGame default constructor How to get started? Will it be writing inside the constructor?
mouse floating (1,2,3, true); m1.push_back (inst Yi);
Which is the better way? In the main I'll just do:
inGame GAME; Thanks:
Update:
No luck
Code> inGame (): M1 {mouse (1,2,3, true) ), Mouse (2,3,4, true), mouse (3,4,5, true)} {} inGame (): M1 {{1,2,3, true}, {2,3,4, True}, {3,4,5, true}} {}
there is an error after "M1 {" that "expected a") and m1 {"} ; - "" expected ";
You need to use a brace -Setted initiator:
inGame (): M1 {{1,2,3, Truth}, {2,3,4, true}, {3,4,5, true}} {}
without C +11, you where std :: vector & Lt; Mouse & gt; Like a container it will need to be used and it is encoded within its body by constructor, or by using a function that is properly started:
std :: vector & lt; Mouse & gt; Make_mice () {std :: vector & lt; Mouse & gt; V; V.reserve (3); V.push_back (mouse (1, 2, 3, true)); V.push_back (mouse (2, 3, 4, true)); V.push_back (mouse (3, 4, 5, true)); Return vi; }
inGame (): m1 (make_mice ()) {}
M1
now a std :: vector & lt; Mouse & gt;
Comments
Post a Comment