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

自古美人都是妖i 提交于 2019-12-02 00:37:38
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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!