Conversion between classes with the same structure in different namespaces

Let's say I have a class A

under a namespace AAA

with a definition like this:

namespace AAA {
    class A
    {
        int x;
        std::vector<double> y;
        A* ptr;
    };
}

      

And I have another class B

with the same structure as below, but it is under a different namespace BBB

,

namespace BBB {
    class B
    {
        int x;
        std::vector<double> y;
        B* ptr;
    };
}

      

I know the correct way to do this is to have only one definition, but now suppose we cannot change the fact that the class AAA::A

and exists BBB::B

. Is there a way to convert an object A

to an object B

?

+3


source to share


1 answer


There is no safe conversion. Best is static_assert (sizeof (A) == sizeof (B), "Different Size") followed by a brave reinterpret_cast.



+1


source







All Articles