Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static_cast from Derived* to void* to Base*

I would like to cast a pointer to a member of a derived class to void* and from there to a pointer of the base class, like in the example below:

#include <iostream>

class Base
{
    public:
       void function1(){std::cout<<"1"<<std::endl;}
       virtual void function2()=0;
};

class Derived : public Base
{
    public:
       virtual void function2(){std::cout<<"2"<<std::endl;}
};

int main()
{
    Derived d;
    void ptr* = static_cast<void*>(&d);
    Base* baseptr=static_cast<Base*>(ptr);
    baseptr->function1();
    baseptr->function2(); 
}

This compiles and gives the desired result (prints 1 and 2 respectively), but is it guaranteed to work? The description of static_cast I found here: http://en.cppreference.com/w/cpp/language/static_cast only mentions conversion to void* and back to a pointer to the same class (point 10).

like image 635
Roberto Avatar asked Oct 27 '13 03:10

Roberto


1 Answers

In the general case, converting a base to void to derived (or vice versa) via static casting is not safe.

There will be cases where it will almost certainly work: if everything involved is a pod, or standard layout, and only single inheritance is involved, then things should be fine, at least in practice: I do not have chapter and verse from the standard, but the general idea is that the base in that case is guaranteed to be the prefix of the derived, and they will share addresses.

If you want to start seeing this fail, mix in virtual inheritance, multiple inheritance (both virtual and not), and multiple implementation inheritance that are non trivial. Basically when the address of the different type views of this differ, the void cast from and back to a different type is doomed. I have seen this fail in practice, and the fact it can fail (due to changes in your code base far away from the point of casting) is why you want to be careful about always casting to and from void pointer with the exact same type.

like image 119
Yakk - Adam Nevraumont Avatar answered Oct 01 '22 06:10

Yakk - Adam Nevraumont