是否有可能创建这样的 C++将您的标准(继承)类包装到应用程序中的宏?

发布于 2024-11-28 11:05:20 字数 2074 浏览 2 评论 0原文

因此,我们有简单的接口基类:

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_;
};

并且我们希望使用这样的类继承它:

#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";
  }
};

如果我们正在创建一个库 - 共享或静态,那么我们只需继承它即可,但是当我们想要创建一个应用程序,我们必须创建一些主要功能,并且可能添加一些包含内容。我想创建一些宏,允许我创建这样的 main,以防类被编译到共享库中。

我希望它看起来像

// 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.hpp" 的包含和“some_includes_for_our_shared_libs.hpp”

因此,在共享库类的情况下,宏将被转换为

#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";
  }
};

应用程序类的情况下,宏将被转换为

#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-base类,并且我们创建了new-服务。我们的主服务器应用程序支持扩展,因此 new-service 可以连接到它,并将其编译到共享库中。但是如果我们想让我们的服务成为一个独立的服务器怎么办?我们需要这样的宏来用 main 包装它,并可能添加或删除一些 #includes

我的观点是,这样任何从我的特殊接口继承的类都可以编译到共享库或可执行文件中,而无需更改任何代码。

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_;
};

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";
  }
};

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.

I hepe it could look like

// 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

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" .

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧桑㈠ 2024-12-05 11:05:20
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 中,您可以无需编辑任何其他代码即可进行编辑。

然后您可以根据自己的喜好定义动物类别。如果您想像这样定义类:

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

那么您可以。但你的类肯定不仅仅只是硬编码一个字符串,所以我希望不同的动物需要单独的定义,即使它们有一定数量的共同点。

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.

你对谁都笑 2024-12-05 11:05:20

我只是让 main() 变得微不足道。喜欢

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

就这样吧。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文