无法将 void* 动态转换为模板类

发布于 2024-09-07 14:07:18 字数 952 浏览 8 评论 0原文

我得到的确切错误是:

Cannotdynamic_cast 'object' (of type 'void*') to type 'class udDator(int)*' (源不是指向类的指针)

这发生在重写的运算符删除内部。我正在尝试创建一个模板化内存管理类,它可以继承到任何其他类,通过引用管理内存。这将取代诸如智能shared_ptr之类的东西,试图使内存管理更加不可见,并且无需额外输入(shared_ptrshared(new someClass())有点长...)。

无论如何,这是相关的代码。如果我忘记提及任何细节,或者没有您需要查看的代码,请告诉我。

重写运算符:

template< class T >
class udSharedMemory
{
public:
    void operator delete( void *object )
    {         
        T *temp = dynamic_cast< T* >( object ); //<------ ERROR!
        assert( temp && "Something went wrong during casting" );
        temp->release();
    }
}

模板化类:

template< class T >
class udDator : public udMemoryManaged, public udSharedMemory< udDator< T > >
{
// stuff
};

模板化类的用法:

udDator< int > *test = new udDator< int >( "5" );

The exact error I'm getting is:

Cannot dynamic_cast 'object' (of type 'void*') to type 'class udDator(int)*'
(source is not a pointer to a class)

This is happening inside an overridden operator delete. I'm attempting to create a templated memory management class that can inherit into any other class, managing memory through references. This would be in place of something like a smart shared_ptr, in an attempt to make memory management even more invisible, and without extra typing ( shared_ptr< someClass > shared( new someClass() ) is kinda long... ).

Anyway, here is the relevant code. If I have forgotten to mention any details, or do not have some code that you need to see, just let me know.

Overridden operator:

template< class T >
class udSharedMemory
{
public:
    void operator delete( void *object )
    {         
        T *temp = dynamic_cast< T* >( object ); //<------ ERROR!
        assert( temp && "Something went wrong during casting" );
        temp->release();
    }
}

Templated class:

template< class T >
class udDator : public udMemoryManaged, public udSharedMemory< udDator< T > >
{
// stuff
};

Usage of the templated class:

udDator< int > *test = new udDator< int >( "5" );

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

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

发布评论

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

评论(3

瞎闹 2024-09-14 14:07:18

在 C++ 中,无法检查指针是否确实包含给定多态类型的对象的地址

您需要有一个指向其中一个基数的指针。例如,从一个多态接口派生所有相关对象,获取 void 指针,将其强制转换为该接口,然后从那里您将能够将其动态强制转换为执行运行时检查所需的类型。

In C++, there's no way to check whether a pointer really contains an address of an object of a given polymorphic type.

You need to have a pointer to one of the bases. For example, derive all relevant objects from one polymorphic interface, take the void pointer, cast it to that interface, and from there you will be able to dynamically cast it to the type you need to perform the run-time check.

乙白 2024-09-14 14:07:18

动态转换需要多态行为,而 void 不具备这种行为。请改用static_cast

Dynamic cast requires polymorphic behavior, which void does not have. Use a static_cast instead.

围归者 2024-09-14 14:07:18

dynamic_cast 只能与
对象的指针和引用。
其目的是确保
类型转换的结果是
所请求的有效完整对象
类。

http://www.cplusplus.com/doc/tutorial/typecasting/

dynamic_cast can be used only with
pointers and references to objects.
Its purpose is to ensure that the
result of the type conversion is a
valid complete object of the requested
class.

http://www.cplusplus.com/doc/tutorial/typecasting/

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