0

This is not duplicate to How to create two classes in C++ which use each other as data?

Here I want two instances to be bound to ecah other at compile-time

I have a template class (they are the same):

template <typename T>
class driver {
  public:
    driver(T& arg):ref(arg){};
  private:
    T& ref;
};

template <typename T>
class handler {
  public:
    handler(T& arg):ref(arg){};
  private:
    T& ref;
};

How to make two instances linked to ech other?

driver<?auto?> driver_instance{handler_instance}
handler<?auto?> handler_instance{driver_instnace};

Imagine I have a driver, which takes reference to some class 'handler' which handles driver's events. On the other hand 'handler' must have reference to 'driver' instance to make calls to driver. I want this two to bind to each other at compile time.

  • Refer to [How to create two classes in C++ which use each other as data?](https://stackoverflow.com/questions/4964482/how-to-create-two-classes-in-c-which-use-each-other-as-data). – Jason Oct 12 '22 at 11:57
  • No this is not. The quetion, you posted, binds classe at run-time – Xeenych Xeenych Oct 12 '22 at 12:00
  • What exactly do you mean with "_bind to each other at compile time_"? Are you assuming that both classes are used only as singletons that refer to one another? If so, why is the reference member required at all instead of referring to the singleton directly? – user17732522 Oct 12 '22 at 12:06
  • I don't want singletons. I may have used pointers instead of references and subscribe/unsubscribe pattern, but that works at runtime. I want bind one to another at compile time – Xeenych Xeenych Oct 12 '22 at 12:11
  • Sorry, but I don't understand what you mean with "_bind at compile-time_". – user17732522 Oct 12 '22 at 12:23

0 Answers0