是否可以创建这样的C ++宏,将标准(继承)类包装到应用程序中? [英] Is it possible to create such C++ macros that would wrap your standard (inherited) class into an application?

查看:112
本文介绍了是否可以创建这样的C ++宏,将标准(继承)类包装到应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我们有简单的接口基类:

So we have simple interface base class:

class animal {
public:
  animal(int age) : age_(age) {
  }
  virtual ~animal(void) {
  }
  virtual std::string get_name(void) {
    return "A generic animal";
  }
  int get_age(void) {
    return age_;
  }
protected:
  int age_;
};

我们希望ti继承它,类似这样:

And we want ti inherit from it with a class like this:

#include "animal.hpp"
#include "some_includes_for_our_shared_libs.hpp"

class puma : public animal {
 public:
  puma(int age) : animal(age) {}
  virtual std::string get_name() {
    return "puma";
  }
};

如果我们正在创建一个库 - 共享或静态它的 ok 为我们继承它,但是当我们要创建一个应用程序,我们必须创建一个主要的函数,并可能添加一些包含。我想创建一些宏,这将允许我创建这样的主要在类中编译不是共享库。

If we are creating a library - shared or static its ok for us just to inherit from it, but when we want to create an application we'd have to create a some main function and probably add some includes. I want to create some macros that would allow me to create such main in case class is compiled not into shared library.

我可以看起来像

// puma.cpp
#include "FILE_WITH_MACROS.hpp"
ANIMAL_MACROS_NAME_HERE class puma : public animal {
 public:
  puma(int age) : animal(age) {}
  virtual std::string get_name() {
    return "puma";
  }
};
//end of puma.cpp

并且该宏将获得类名并创建main将创建并调用我们的类,好像它是调用一般的animall类或者只是添加包括animal.hppsome_includes_for_our_shared_libs.hpp

And that macros would get class name and create a main that would create and call our class as if it was calling general animall class or just add includes for "animal.hpp" and "some_includes_for_our_shared_libs.hpp" .

因此,如果共享的lib类被宏转换为

So in case of shared lib class would be turned by macros into

#include "animal.hpp"
#include "some_includes_for_our_shared_libs.hpp"

class puma : public animal {
 public:
  puma(int age) : animal(age) {}
  virtual std::string get_name() {
    return "puma";
  }
};

如果应用程序类将被宏转换为

And in case of application class would be turned by macros into

#include <iostream>
#include "animal.hpp"


class puma : public animal {
 public:
  puma(int age) : animal(age) {}
  virtual std::string get_name() {
    return "puma";
  }
};

int main()
{
int default_age = 4;
puma *an_animal = new puma(default_age);
std::cout << "anmal "<< an_animal->get_name << " created. Its age is " << an_animal->get_age << std::endl;
std::cin.get();
}

用法:假设我们有一个 service- / code>类,我们创建了 new-service 。我们的主服务器应用程序支持扩展,所以 new-service 可以连接到它在cese它被编译成共享库。但是如果我们想让我们的服务成为一个独立的服务器?比我们需要这样的宏包装它与 main ,并可能添加或删除一些 #includes

Usage: imagine we have a service-base class and we created new-service. Our main server app supports extensions so new-service can be connected to it in cese it is compiled into shared library. But what If we want to make our service into a stand alone server? than we would need such macros to wrap it with main and possibly add or remove some #includes.

我的观点是让任何从我的特殊接口继承的类都可以编译成共享库或exequtable,而不需要任何代码更改。

my point is to make it so that any class that inherits from my special interface could me compiled into shared library or exequtable with out any code changes.

推荐答案

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();
}

然后编译(你的编译器的等效项) DSERVICECLASS = puma ,或者有一个叫做config.h的文件,它被 main 包含,编辑任何其他代码。

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.

这篇关于是否可以创建这样的C ++宏,将标准(继承)类包装到应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆