I have a class, which defines a new nested class as well as a single member of that class.
This nested class has an operator that needs access to the parent.
The nested class won't be used outside of these confines. (I can even prevent this by making the class definition private).
What is the "clean" way to make this nested class access it's parent ?
I can solve it with some messy casting, but I was wondering if there is a cleaner way to do this than:Code:struct X { private: // Make Y unaccessible for anything but X. struct Y { int operator()() const { return yy * x; // This doesn't compile as it is shown here. } int yy; }; public: // Data members are public. int x; Y y; double x2; }; // In the rest of the code it would be accessed as: static const X x = { 2, 4, 1.24 }; // Both X and X::Y need to be aggregate (by contract) int test = x.y(); // should return 8. with above sample.
Code:int operator()() const { const X* dummy = (X*)NULL; const X* px = (X*)((char*)this - (char*)&dummy->y); return yy * px->x; }




Reply With Quote