pointers - C++ member of class updated outside class -


i have question pointers , references in c++. programmer programs in c# , php.

i have 2 classes (for now) following. x/y in controller continuously changing want them date in commands. have multiple commands forward, turn, backward etc.

when make commands give them controller state (x, y) of controller updating every second.

how can fix controller attribute in commands getting updated every second?

class forward : icommand {     controller ctrl;      void execute() {         int currentx = ctrl.x;         int currenty = ctrl.y;         //check here current location , calculate has go.      } }  class controller  {     int x;      int y;       void executecommand(icommand command) {         command.execute();     } } 

main.cpp

controller controller;  forward cmd1 = new forward(1, controller); turn cmd2 = new turn(90, controller); forward cmd3 = new forward(2, controller);  controller.execute(cmd1); controller.execute(cmd2); controller.execute(cmd3); 

i have read pointers , references , think have use don't know how use in situation.

(code can have syntax errors that's because typed over. working further except updating).

if use references rather copy objects can see changes.

#include <iostream> using namespace std;  class icommand { public:     virtual ~icommand() = default;     virtual void execute() = 0; };  class controller { public:     int x = 0;     int y = 0;      void executecommand(icommand & command) {         //                       ^-------         command.execute();     } };//,--- semicolons required  class forward : public icommand //note public {     const int step;     controller ctrlcopy;     controller & ctrlreference;  public:     forward(int step, controller & ctrl) :         step(step),         ctrlcopy(ctrl),      //this copy of object         ctrlreference(ctrl)  //this reference object     {      }      void execute() {         std::cout << "copy: " << ctrlcopy.x << ", " << ctrlcopy.y << '\n';         std::cout << " ref: " << ctrlreference.x << ", " << ctrlreference.y << '\n';         //check here current location , calculate has go.          ctrlcopy.x += 10;         ctrlreference.x += 10;     } };//<--- semicolons required   int main() {     controller controller;      forward cmd1(1, controller);     //turn cmd2(90, controller); //left op     forward cmd3(2, controller);      controller.executecommand(cmd1);     //controller.executecommand(cmd2);     controller.executecommand(cmd3);      //do again show copy , reference difference     std::cout << "once more, feeling\n";     controller.executecommand(cmd1);     controller.executecommand(cmd3); } 

giving

copy: 0, 0  ref: 0, 0 copy: 0, 0  // [1]  ref: 10, 0 // [2] once more, feeling copy: 10, 0  ref: 20, 0 copy: 10, 0  ref: 30, 0 

1 shows copy has x , y of 0, while reference shown in [2] has moved stated step (in controller.executecommand(cmd3))

note, don't need use new make work (don't forget delete if use new).

also, void executecommand(icommand command) takes reference instead otherwise by-value copy "slicing" (e.g. see here)


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -