如何获取从 C++ 中的特定类继承的所有已声明类的列表

发布于 2024-12-03 15:32:46 字数 685 浏览 1 评论 0原文

我知道这在 C++ 中是不可能的,但也许有一个工具链可以生成具有函数的代码,该函数在调用时会为我提供所有这些类的列表。例如,在多个文件中,我有类似的内容:

class MyClass : public ParticularClass {
    ....
}

class MyClass2 : public ParticularClass {
    ....
}

然后,在运行时,我只想要一个指向该类的单个实例的指针。假设我生成的代码如下所示:

void __populate_classes() {
    superList.append(new MyClass());
    superList.append(new MyClass2());
}

另外,superList 的类型为 List。另外,我将使用 Qt,并且 PspecialClass 将由 QObject 派生,因此我无论如何都可以获取类的名称。我基本上需要对类进行内省,因此我的内部代码并不真正关心新定义的类型。

那么,有没有办法使用一些工具链生成此代码?如果单独使用 qmake 就可以实现,那就像是锦上添花:)

非常感谢您的宝贵时间。

I know that isn't exactly possible in C++, but maybe a toolchain that can generate code which has a function, which when called gives me a list of all those classes. For example, across multiple files I have stuff like:

class MyClass : public ParticularClass {
    ....
}

class MyClass2 : public ParticularClass {
    ....
}

Then, during runtime, I just want a pointer to single instances of the class. Let's say my generated code looks something like this:

void __populate_classes() {
    superList.append(new MyClass());
    superList.append(new MyClass2());
}

Also, superList would be of type List<ParticularClass*>. Plus, I'll be using Qt and ParticularClass will be QObject derived, so I can fetch the name of the class anyways. I need to basically introspect the class, so my internal code doesn't really bother much about the newly defined type.

So, is there a way to generate this code with some toolchain? If it is possible with qmake alone, that'd be like icing on the freaking cake :)

Thanks a lot for your time.

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

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

发布评论

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

评论(3

葵雨 2024-12-10 15:32:46

Doxygen 在这方面做得很好——离线。各种 IDE 在这方面做得很好——离线。编译器不会这样做。编译器不需要或使用这些知识。

Doxygen does a nice job at doing this -- offline. Various IDEs do a nice job at this -- offline. The compiler does not do this. Such knowledge is not needed or used by the compiler.

夏了南城 2024-12-10 15:32:46

在工作中,我使用一个名为 Understanding 4 C++ 的工具。它是一个可以帮助您分析代码的工具。它会很容易地做到这一点。
但我最喜欢的部分是它附带了 C 和 Perl API,它允许您利用“理解”封装的抽象语法树并编写您自己的静态分析工具。我使用这个 API 编写了大量工具。

无论如何,它是由 SciTools 编写的。 http://scitools.com 我不为他们工作。我只是全心全意地喜欢他们的产品。事实上,几年前我编写了一个 C# API 来包装他们的 C API,并将其发布在 CodePlex 上。使用 C 或 Perl 编写静态分析工具当然胜过使用 C 或 Perl。

Here at work I use a tool called Understand 4 C++. It is a tool that helps you analyze your code. It will do this quite easily.
But my favorite part is it comes with a C and Perl API which allows you to take advantage of the abstract syntax tree that 'understand' encapsulates and write your own static analysis tools. I have written tons of tools using this API.

Anyways, it's written by SciTools. http://scitools.com and I don't work for them. I just wholeheartedly like their product. In fact I wrote a C# API that wraps their C API and posted it on CodePlex a few years ago. Sure beats using C or Perl to write static analysis tools.

只为一人 2024-12-10 15:32:46

我不认为你想做的事是个好主意。那些在你之后维护代码的人将很难理解它。

也许您会尝试看看如何在计划 C++ 中做到这一点。我想到的一种可能的解决方案是实施工厂设计模式。您可以迭代工厂中的所有数据类型,然后将其添加到 superList 中。

无论如何,如果您总是在一行中声明继承,那么使用 ack (简单的 grep 替换)可以完成这项工作:

ack ": *public ParticularClass" *.h 

I don't think what you're trying to do is a good idea. Those who will maintain code after you will have hard times to understand it.

Maybe instead of it you'll try see how you can do it in plan C++. One possible solution which comes to mind i to implement factory design pattern. Than you can iterate over all data types in factory and add then to superList.

Any way, using ack (simple grep replacement) can do the job if you always declare the inheritence in one line:

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