基类和派生类的运行时类型信息

发布于 2024-12-02 16:43:24 字数 362 浏览 2 评论 0原文

我正在尝试扩展一些我无法访问的类的功能。

基本上我正在尝试创建一个 toString/toXML 方法用于日志记录目的。我想创建一个单例(本质上是一个映射),我可以在其中注册不同的函数并以某种方式让它们全局可用,这样我就可以拥有类似 string Singleton::toString(void* or AbstractObject*)< /code> 它将根据类型从地图中选择正确的方法。

虽然我可以使用 typeid 获取类型信息,但我希望能够为基类实现它,然后让所有派生类使用该方法,除非我有“更接近”/更合适的方法。

这可能吗,或者我应该更改为不同的方法(模板可以做到这一点)吗?不幸的是,我无法访问有问题的类,因为它们大多数来自第三方库。

I'm trying to extend the functionality of some classes that I do not have access to.

Basically I'm trying to create a toString/toXML method for logging purposes. I thought of creating a singleton (essentially a map) to which I can register the different functions and have them globally available in a fashion, so I can have something like string Singleton::toString(void* or abstractObject*) which would pick the correct method from the map depending on the type.

While I can get type information with typeid, I want to be able to implement it for a base class and then have all derived classes use that method, unless I there is a 'closer' / more appropriate method.

Would that be possible, or should I change to a different method (can templates do that)? I can not access the classes in question unfortunately, as most of them are from 3rd party libraries.

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

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

发布评论

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

评论(1

云醉月微眠 2024-12-09 16:43:24

我发现以下是一种非常简单的方法,可以让我跟踪我定义的基类和派生结构或类的类型。

_kind 不一定是字符串。事实上,它可能应该是一个枚举。然而,由于当我想出这个时,我正在设计一个相当高级的程序,字符串对我来说足够快了。

struct base
{
protected:
    string _kind;
public:
    base()
    {
        _kind = "base";
    }

    ~base()
    {
    }

    string kind()
    {
        return _kind;
    }
}
struct derived : base
{
    derived()
    {
        _kind = "derived";
    }
    ~derived()
    {
    }
}

对于内置结构,也许重载 toString 和 toXML 方法并让编译器决定?

string toString(structure1 A)
{
...
}
string toString(structure2 A)
{
...
}

对于派生结构来说,这可能会有点令人困惑,但是我认为编译器会选择叶结构作为类型而不是基础类型。

例如,如果您有类型 A : B : C : D,并且为 C 和 A 定义了函数 f。如果您输入 B 类型的变量,则它应该自动类型转换为 A。如果您输入类型的变量C,那么它将使用C类型的函数。如果您输入D类型的变量,那么它应该自动类型转换为C类型,然后应该调用C函数。

您也可以尝试本质上做同样事情的模板。

template <class t>
string toString(t A)
{
...
}

干杯,
内德

I have found that the following is a very simple way for me to keep track of the types of base and derived structures or classes that I define.

_kind does not have to be a string. In fact, it should probably be an enum. However, since I was designing a rather high level program when I came up with this, strings were fast enough for me.

struct base
{
protected:
    string _kind;
public:
    base()
    {
        _kind = "base";
    }

    ~base()
    {
    }

    string kind()
    {
        return _kind;
    }
}
struct derived : base
{
    derived()
    {
        _kind = "derived";
    }
    ~derived()
    {
    }
}

For built-in structures, perhaps overload the toString and toXML methods and let the compiler decide?

string toString(structure1 A)
{
...
}
string toString(structure2 A)
{
...
}

This might get a little confusing for derived structures, however I think the compiler will choose the leaf structure as the type instead of the base.

For example, if you have types A : B : C : D, and function f is defined for C and A. If you input a variable of type B, then it should be auto typecasted to A. If you input a variable of type C, then it will use the function for type C. If you input a variable of type D, then it should be auto typecasted to type C and then the C function should be called.

You might also try templates which essentially does the same thing.

template <class t>
string toString(t A)
{
...
}

Cheers,
Ned

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