Consider the following C++ code, where member foo
has a private type:
class Object {
private:
struct MyPrivateThing {
long double Member;
};
using Type = MyPrivateThing;
public:
Type foo = {32.45L};
// MyPrivateThingFoo also works
};
int main() {
// Object::Type f = {34.567L}; ERROR: Object::Type is private
decltype(Object::foo) thingy = {945.67L}; // Fine
auto x = Object().foo;
return x.Member * thingy.Member;
}
Although this code is legal C++ and compiles fine, it seems a bit counter-intuitive —Object::Type
is private and not accessible outside the class, yet we can still indirectly refer to it by using auto
to get a variable of that type.
It would seem that the more straightforward thing to do would be to just make every public member have a public type.
Can you think of any valid use-cases for this pattern?