Mert Mertce
Mert Mertce

Reputation: 1634

In C++ should derived class pointer point to base class pointer?

I've very rare error and to understand the reason I need to know some facts of C++. Let me explain the problem:

There is such structure:

struct Base;                 // not polimorfic
class Derived : public Base; // has pure virtual
class ImplDerived : public Derived    // implements pure virtuals

some_function(Base* base);
int main() {
    ImplDerived impd;
    some_function(&impd);
}

Now, what's happening is this:

Questions:

Thanks.

Compilers:

EDIT (RESULT):

After all tests, we have decided that this is a compiler error. Casting just does not work correctly, i) for non-polymorphic base, ii) for multiple inheritance.

Upvotes: 0

Views: 313

Answers (2)

eerorika
eerorika

Reputation: 238401

AFAIK, the offset between base and derived pointer is permittible, right?

It is permitted, yes. In fact, it's mandatory if the derived class has multiple (non-empty) base classes, since all of the base class sub objects cannot share the same address. But even a sole base class sub object might not share the derived object's address, as you have observed.

If it is, then should not the implicit casting when passing the pointer to the some_function find where the base is?

Yes. When the derived pointer is implicitly converted to the base pointer, it should be pointing to the base sub object.

If it should, then would it be some error in compilation?

Maybe, the example code should be just fine, but it's incomplete.

Upvotes: 0

Serge Rogatch
Serge Rogatch

Reputation: 15070

No, pointers to base and derived classes do not have to match. Especially this happens when multiple inheritance is used. In your case the reason seems different. Because the base class is not polymorphic, it does not have a virtual table pointer as its hidden member. However, the polymorphic derived classes start to have virtual table pointer. I guess you are compiling in 32-bit, so that's where 4 bytes offset arises: that pointer is 32-bit.

dynamic_cast should solve your problem.

Upvotes: 3

Related Questions