-1
\$\begingroup\$

I am writing a program with C++ and since it is going to be big and a lot of people will edit this, I am going to use the MVC pattern. The question is very easy.

There is a controller that is the intermediate between the model (.cpp and .h files) and the view (made with Qt). Since the controller is only one and it has to manage everything during the lifetime of the app, do I have to deny copy constructor and copy assignment like this?

class Controller {
 public:
  Controller(const Controller& x) = deltete;
  Controller& operator=(const Controller& x) = delete;
}

Is this still flexible? Because I don't see the sense of having 3 instances of a controller and each of them manage a different part of the program. There must be only one, am I correct?

\$\endgroup\$
  • 1
    \$\begingroup\$ Looks like a syntax error to me - unless your implementation has a macro called deltete. \$\endgroup\$ – Toby Speight Dec 8 '17 at 9:32
1
\$\begingroup\$

It sounds like what you want is a hard-enforced singleton.

With an interface like the following, you can provide a hard guarantee that only a single instance of Controller will ever exist:

class Controller {
public:
  static Controller& instance();

private:
  Controller();
};

I am personally partial to Meyer's singleton for the implementation:

// in .cpp file:

Controller& Controller::instance() {
  static Controller the_instance;
  return the_instance;
}
| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.