-
classes within classes?
yet another question.... this program is driving me nuts. anyway...
i have a class (CMessages) that i want to have encapsulated within another class (CUser). i also want to have the encapsulated class have access to the members of the outer class, but when i try to use them i get errors saying "illegal implicit conversion from CUser::CMessages to CUser." this is something i've done plenty of in java and c# but i have no idea how to do it in c++, or if it is even possible.... can anyone help?
... praying that 2 answered questions in 1 day isn't too much to ask.....
-
It does not work that way in C++. In Java, I believe the inner class has access to the members of the specific instance of the outer class. In C++, nesting a class only defines the scope in which it lives. There is no specific instance of the inner class tied to the outer class, unless you specifically make one, and even then the inner class instance has no way of referring to the outer class instance's specific members.
If you make the inner class a friend, it will have access to the private data of the outer class, but you still must somehow provide it a reference to an existing instance to work on.
-
ah right... i completely forgot about the whole friend concept. again, thanks so much daved. i really appreciate the help.
-
An inner class in C++ that you declare as a friend works the same as a static inner class in Java.