Is it possible to create such C++ macros that would wrap your standard (inherited) class into an application?

前端 未结 2 656
猫巷女王i
猫巷女王i 2021-01-22 09:06

So we have simple interface base class:

class animal {
public:
  animal(int age) : age_(age) {
  }
  virtual ~animal(void) {
  }
  virtual std::string get_name(v         


        
相关标签:
2条回答
  • 2021-01-22 09:36

    I'd just make it so that the main() is trivial. Like

    int main()
    {
        int default_age = 4;
        return puma(default_age).run();
    }
    

    and leave it at that.

    0 讨论(0)
  • 2021-01-22 09:39
    int main()
    {
    int default_age = 4;
    SERVICECLASS *an_animal = new SERVICECLASS(default_age);
    std::cout << "anmal "<< an_animal->get_name << " created. Its age is " << an_animal->get_age << std::endl;
    std::cin.get();
    }
    

    Then compile with (your compiler's equivalent of) -DSERVICECLASS=puma, or have a file called something like "config.h" which is included by main, and that you can edit without having to edit any other code.

    Then you can define your animal classes however you like. If you want to define classes like this:

    #define STRINGIZE_(ARG) #ARG
    #define EXPAND_AND_STRINGIZE(ARG) STRINGIZE(ARG)
    
    class SERVICECLASS: public animal {
     public:
      SERVICECLASS(int age) : animal(age) {}
      virtual std::string get_name() {
        return EXPAND_AND_STRINGIZE(SERVICECLASS);
      }
    };
    

    then you can. But surely your classes do more than just hard-code a string, so I'd expect different animals to need separate definitions even if they have a certain amount in common.

    0 讨论(0)
提交回复
热议问题