c++ - How should I conceal helper classes from the rest of the world? -


i have few classes. 1 class supposed visible needs it, , rest helper classes (well, of them structs) should accessible principal class. had implemented nested class inside principal class, starting make header file cumbersome added more helper classes. i've put helper classes in separate namespace forbidding kind of name, suspect there's better way. i'd use anonymous namespace, can't take helper classes out of header, because appear member variables in principal class.

here's simplified version of construction that's in header file now, minus include guards:

namespace __topsecretvisiblenamespace {     class invisible{     }; }  using namespace __topsecretvisiblenamespace; class visible {     public:         visible();     private:         invisible sneaky; }; 

any thoughts?

you can't literally 'conceal' them, can make them inaccessible other class using protected modifier. if, reason, don't want use protected on class members, can use on construtor. in case, non-friend classes cannot access member of class, including public ones.

 class invisible {     friend class visible;     protected:     invisible() {     }     public:     // though public, cannot called classes other visible, because can't instantiate class.     int calc(int n)       {         return n*n;     }     int i;   };  class visible {     public:     visible(int i)     {         invisible o;         x=o.calc(i);     }     int x; }; 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -