Unlike other examples of this error message i already have a pointer to A
and want to retrieve the actual child class.
This kind of arrangement is part of some C++ wrapped C code there A
is some POD C structure (whatswhy no dynamic cast) and test
is some callback in C that calls C++ functionality and to retrieve the correct object the cast should be used.
But to prevent C++ user code messing the C-Baseclass i would like to have the inheritance protected
.
MSVC does not complain about this but g++ does!? Which one is correct from the standards point of view and why?
#include <iostream>
using namespace std;
// plain C structure
struct A{
int i;
};
// some C++ Wrapper class
struct B: protected A{
A* get() { return this; }
void print(){cout << i << endl;}
};
extern "C" {
// C callback that gives it this pointer
void test(A* io_self){
auto b2 = static_cast<B*>(io_self);
b2->print();
}
}
int main()
{
B b;
test(b.get());
return 0;
}
gives:
$g++ -std=c++11 -o main *.cpp
main.cpp: In function ‘void test(A*)’:
main.cpp:21:43: error: ‘A’ is an inaccessible base of ‘B’
auto b2 = static_cast<B*>(io_self);
^