指向对象(C ++)开始的指针 [英] Pointer to the start of an object (C++)

查看:148
本文介绍了指向对象(C ++)开始的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来获取C ++中对象的开始的指针。这个对象在模板中使用,所以它可以是任何类型(多态的或非多态的),并且可能是使用多重继承的对象。

I need a way to get a pointer to the start of an object in C++. This object is used inside a template so it can be any type (polymorphic or not) and could potentially be an object that uses multiple inheritance.

我发现本文介绍了一种方法(请参阅动态转换部分)使用typeid和一个dynamic_cast到void *在T是一个多态类型的情况下。

I found this article which describes a way to do it (see the section called "Dynamic Casts") using typeid and a dynamic_cast to void* in the case that T is a polymorphic type.

这在MSVC上工作得很好,但是在GCC(4.x)

This works perfectly well on MSVC, however on GCC (4.x) it seems to fall on its arse and spits out a compiler error when it is used with a non-polymorphic type.

有没有人知道一种方法:

Does anyone know a way to:


  • 使GCC正常运行,并正确评估typeid

  • 或者通过另一种方法来编译GCC

下面是我目前使用的代码尝试实现这个。

Below is the code I am currently using to try and achieve this.

template <typename T>
void* dynamicCastToVoidPtr(T *const ptr)
{
    // This is done using a separate function to avoid a compiler error on some 
    // compilers about non-polymorphic types when calling startOfObject
    return dynamic_cast<void*>(ptr);
}

template <typename T>
void* startOfObject(T *const ptr)
{
    // In cases of multiple inheritance, a pointer may point to an offset within 
    // another object
    // This code uses a dynamic_cast to a void* to ensure that the pointer value 
    // is the start of an object and not some offset within an object
    void *start = static_cast<void*>(ptr);
    if(start)
        typeid(start = dynamicCastToVoidPtr(ptr), *ptr);
    return start;
}

template <typename T>
void doSomethingWithInstance(T *const instance)
{
    // Here is where I need to get a void* to the start of the object
    // You can think of this as the deleteInstance function of my memory pool
    // where the void* passed into freeMemory should point to the
    // start of the memory that the memory pool returned previously
    void *start = startOfObject(instance);
    if(start)
        allocator->freeMemory(start);
}

感谢。

推荐答案

我找到了一个来自另一个问题的解决方案,如果类型是多态的,我可以在编译时工作,然后可以使用模板专门化来使用正确类型的转换。显然,如果编译器在子对象之间添加填充,这个方法可能会中断,但我希望在一些已知的情况下添加一些编译时断言来捕获它。它在MSVC和GCC上编译并正确运行。

I've found a solution from another question that lets me work out at compile time if a type is polymorphic and I can then use this with template specialisation to use the correct type of cast. Apparently this method might break if the compiler adds padding between sub-objects, but I can hopefully add some compile time asserts on some known cases to catch that. It does compile and run correctly on both MSVC and GCC.

这是一个类型是多态的代码。

This is the code to work out if a type is polymorphic.

#define SIMPLE_POLYMORPHIC(TYPE, POLYMORPHIC)   \
    template <>                                 \
    struct IsPolymorphic<TYPE>                  \
    {                                           \
        static const bool value = POLYMORPHIC;  \
    };

template <typename T>
struct IsPolymorphic
{
    struct Derived : public T { virtual ~Derived(); };
    static const bool value = (sizeof(Derived) == sizeof(T));
};

SIMPLE_POLYMORPHIC(int, false);
SIMPLE_POLYMORPHIC(unsigned int, false);
// ... do this for all intrinsic or non-derivable types

template <typename T, bool isPolymorphic = IsPolymorphic<T>::value>
struct StartOfObject
{
    static void* getStart(T *const ptr)
    {
        return static_cast<void*>(ptr);
    }
};

template <typename T>
struct StartOfObject<T, true>
{
    static void* getStart(T *const ptr)
    {
        if(ptr)
            return dynamic_cast<void*>(ptr);
        return NULL;
    }
};

和一个测试用例。

#define CLASS_STUFF(CLASS)      \
    public:                     \
        CLASS() {}              \
        virtual ~CLASS() {}     \
        int m_##CLASS;

class A
{
    CLASS_STUFF(A);
};

class B : public A
{
    CLASS_STUFF(B);
};

class C
{
};

#include <iostream>

int main()
{
    std::cout << IsPolymorphic<A>::value << std::endl;
    std::cout << IsPolymorphic<B>::value << std::endl;
    std::cout << IsPolymorphic<C>::value << std::endl;
    std::cout << IsPolymorphic<int>::value << std::endl;

    StartOfObject<A>::getStart(new A());
    StartOfObject<B>::getStart(new B());
    StartOfObject<C>::getStart(new C());
    StartOfObject<int>::getStart(new int());

    return 0;
};

这篇关于指向对象(C ++)开始的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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