0

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 as explicit: compiler starts saying "illigal storage class" on the struct type in the operator definition.
  • without the explicit keyword, the compiler still recognize the conversion as inaccessible in the point it is called (i used static_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);
    }
};
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
  • 4
    Show your code with a minimal example. – quantdev Aug 05 '14 at 08:18
  • 4
    It is a poor design. On the one side, you use protected inheritance to hide members of struct, on the other, you want them still be accessible via upcast. Weird. Try to revise your architecture and consider using aggregation instead of inheritance. – Mikhail Aug 05 '14 at 08:24
  • You might find the contrast between [VC++ 2013 docs](http://msdn.microsoft.com/en-us/library/wwywka61.aspx) (which detail `explicit`) and [2010 docs](http://msdn.microsoft.com/en-us/library/wwywka61%28v=vs.100%29.aspx) on conversion operators enlightening (i.e. if you want C++11 features, don't use a 2010 compiler). – Tony Delroy Aug 05 '14 at 08:33
  • Also note, that conversion operator will return by value, thus it cannot be considered as a cast. – Mikhail Aug 05 '14 at 08:33
  • @Mikhail eh? Casts are conversions and casting to a non-reference type yields an rvalue. E.g. `(int)6.5`. I'm not sure what you are trying to say – M.M Aug 05 '14 at 08:36
  • @Mikhail I've already implemented it via aggregation. The question is: why it should not compile in the other way. – nyarlathotep108 Aug 05 '14 at 08:55
  • @quantdev sorry, you are right. Example provided – nyarlathotep108 Aug 05 '14 at 09:22
  • @TonyD well thanks, al least the first point is solved, I did not remember that "explicit cast operator" was a C++11 feature – nyarlathotep108 Aug 05 '14 at 09:30
  • @MattMcNabb ok, I misused the terminology. I meant that hierarchy upcast and downcast actually do not create any new object, while conversion operator will do. – Mikhail Aug 05 '14 at 10:04
  • @nyarlathotep108 It doesn't matter where you are trying to upcast `Data` to `DataFromC`. It is inaccessible at any point. – Mikhail Aug 05 '14 at 10:06
  • @Mikhail yes but why? It should be allowed inside the scope of `Data` since the inheritance is just `protected`. – nyarlathotep108 Aug 05 '14 at 12:27
  • @nyarlathotep108 It seems like you can declare a getter function like `DataFromC* AsDataFromC() { return this; }` and it will work. Not sure why conversion operator is not working. I recommend not to think too much about it. No one will write this way in a normal program anyway. – Mikhail Aug 05 '14 at 19:47
  • @Mikhail ok, the method as the expected behaviour, that confirms conversion operator must act following some special rule in this case. – nyarlathotep108 Aug 06 '14 at 08:35

0 Answers0