I am trying to use protected
inheritance to hide a C-style struct
members.
By doing this, my derived class
is now capable to access everything from the struct
while hiding it from the rest of the program, but it has a cost:
compiler won't allow me anymore to implicit cast from this derived class
to the base C-style struct
.
So, to enable again the feature, I added a public
conversion operator
in the derived class
. But here come the weird things:
- first of all, I could not flag the
operator
asexplicit
: compiler starts saying "illigal storage class" on thestruct
type in theoperator
definition. - without the
explicit
keyword, the compiler still recognize the conversion as inaccessible in the point it is called (i usedstatic_cast<T>()
).
Any idea? (I'm using Visual Studio 2010)
Code example:
struct DataFromC
{
int a, b, c;
};
class Data : protected DataFromC
{
public:
explicit operator DataFromC()
{
return (DataFromC)(*this);
}
};