1

I've got this classes:

class A{};
class B
{
public:
    A* ptr_obj;
    A obj;
    operator A*&()
    {
         return ptr_obj;
    }
    operator A&()
    {
         return obj;
    }
};
class C : public B {};

Now I have a pointer to a obj of type C

C* ptr_c;

And two other pointers, one of type B and one of type A

B* ptr_b;
A* ptr_a;

I want to cast ptr_c and assign it to ptr_a and ptr_b:

ptr_b = static_cast<B*>(ptr_c);
ptr_a = static_cast<A*>(*ptr_c); //calls B::operator A*&()
ptr_a = &static_cast<A>(*ptr_c); //calls B::operator A&()

I cant figure out which is the runtime overhead of the above expressions. I think the compiler can easily optimize this, but can it always do it?

In the first case I just point to the base class so I think there isn't any overhead (maybe the compiler can add some offset to the pointer) but in the others I don't have any single idea of what is really happening.

Liuka
  • 289
  • 2
  • 10

0 Answers0