1

Powerful reflection capabilities make implementing nice architectural design patterns such as MVC and Dependency Injection very simple in languages like Java and Kotlin. In particular, reflective tags and annotations make it possible for a DI framework to "feel out" the layout of the program, "catch" controllers and views as they are spawned, and inject them with dependencies accordingly with very little boilerplate or overhead. Think Spring in Java/Kotlin: doing an MVC with its @Controller annotations is cheap, easy, and clean like you wouldn't believe. Android with Hilt and Dagger is pretty much the same way, too, though it "prefers" MVVM over MVC.

But in C++, we have none of that. Reflection doesn't exist there!

So how can one make a relatively neat/clean MVC implementation with dependency-injected model services? Are there existing frameworks? I've been playing around for a long time with various iterations of thus using Google Fruit (https://github.com/google/fruit) as a dependency injection system, but I've yet to find a fully-satisfactory arrangement; a lot seem to make a mess particularly of the app entry point stuff. Ideally the Views should be able to be extended into common GUI widget systems (like Qt) while keeping the Controller (event response) and Model (domain layer, services) agnostic of the particular widget system in use. I'm not expecting it to be quite as neat as Android and Spring - but how neat can we go?

Or should one just ditch C++ (despite this being a compute-intense app) altogether? :D

5
  • This is an interesting problem, but it is probably larger than what this community typically supports. This is a big problem, indeed. A proper answer to this question involves designing an MVC framework from the ground up. We could help with smaller problems related to designing the framework, but this question will likely get closed as needing focus. Aug 27, 2023 at 12:02
  • But to be fair, I've had this same question too. Class reflection really cleans things up, and when a language doesn't support reflection it becomes difficult to find your way. Aug 27, 2023 at 12:04
  • @Greg Burghardt: So it seems that this is actually a coding project in its own right, that would best be separated from the main project it's intended for, then? Thanks. Where/what should I go to for further advice? Aug 27, 2023 at 12:58
  • Something like this requires research and forums that support general discussions. The Q&A format for all Stack Exchange communities is not a good fit, however you could try The Whiteboard, a general chat on this site. Aug 27, 2023 at 13:22
  • @Greg Burghardt: Yes. I was more asking if you knew of a specific such forum site, that pertains to software design using languages like C++, perhaps outside this network. Aug 27, 2023 at 14:01

1 Answer 1

2

I'm surprised nobody has mentioned the ancestor of a lot of MVC: Microsoft, in particular the MFC era. All the original work was done in C++.

Some historical references: https://www.codeproject.com/Articles/30291/An-Introduction-to-a-Model-View-Controller-Impleme ; https://www.codeproject.com/Articles/1197973/Demystifying-and-Simplifying-MVC-Frameworks

Now, of course, most of the frameworks they provide are Windows-only. But it should be possible to learn from them, and I don't think there are any Windows-specific language features involved. So everything could be built in C++ on another platform (you didn't specify which platform?)

C++ does have RTTI, but in practice most of the Microsoft frameworks either made clever use of macros/templates to ensure everything got registered with DI containers, or use COM objects as a reflection-like system built on top of C++ (and even accessible from C if you are willing to put up with a slog of macros).

1
  • Thanks! These are quite helpful and it pretty much suggests I was on the right track with some stuff I was toying with while waiting for answers, which was basically to kind of "fake" your own reflection system in some way (analogous to Qt with the MOC, or Boost.Describe, or ... though those all seem to be a lot more heavyweight than what's needed for this simple application alone). Aug 28, 2023 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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