0

I have a core code that I don't want to hack. There's like 30 objects in c++ that are updated with help of GitHub.

My software takes this code and implements a lot of things and change others. What I need to do is to somehow use a class and method if this code is available in another namespace or folder or use the original if not.

Also, I can't change the core (with exception of main).

I aim to have an organized code struct with relativity maintainability.

Example:

#include <iostream>

namespace core {
  class foo1 {
    public:
      static void fun1(){
        std::cout << "fun1 from foo1 from core" << std::endl;
      }
      static void fun2(){
        std::cout << "fun2 from foo1 from core" << std::endl;
      }
  };
  class foo2 {
    public:
      static void fun3(){
        std::cout << "fun3 from foo2 from core" << std::endl;
        core::foo1::fun1();
      }
  };
};

namespace hack {
  class foo1 {
    public:
      static void fun1(){
        std::cout << "fun1 from foo1 from hack" << std::endl;
      }
  };
  class foo2 {
    public:
      static void fun3(){
        std::cout << "fun3 from foo2 from hack" << std::endl;
      }
  };
};

int main(void){
  XXX::foo2::fun3();
  XXX::foo1::fun2();
}

In this example, I'd like to the linker dynamically find if there's the method "fun3" in the namespace "hack" and class "foo2", if true, use it, if not, try in namespace "core". Same for "fun2" in "foo1".

Therefore, the output should be:

fun3 from foo2 from hack
fun2 from foo1 from core

Remember, I don't want to hack core (maybe just main function), all of this is outside the code. It's some kind of interception. I don't know what to search for. All the terms I've tried didn't get any results. I don't know if polymorphism could be used either. Another problem it's that the compiler could not find some method in the file it could find some error (for the hacked class in the namespace hack). I know that in PHP there's a way to do that.

  • Virtual functions and overrides seems like a better approach to me. Also possibly subclassing. – Dave S Feb 09 '22 at 01:06
  • Yeah, but how can I make the instantiation of a subclass B (of base class A) while in class C (that create a A object) ? How could I use virtual in this case? – Gabriel Bernardes de Carvalho Feb 09 '22 at 01:15
  • I'm saying instead of "hack" creating classes with the same name it would be subclassing and overriding base classes in "core". What exactly are you trying to accomplish by having 2+ versions of the same set of classes? – Dave S Feb 09 '22 at 17:25
  • I'm using a open source code server that updates from time to time and my server is a custom one. When I need to update, it's a mess. – Gabriel Bernardes de Carvalho Feb 10 '22 at 12:26

0 Answers0