![]() |
|
c++ nested class protyping |
oscar_spencer
Member #8,914
August 2007
|
How does one prototype a nested class? for example with this in one file class Outer { public: class Inner { ... }; ... }; i prototype Outer in another file. class Outer;
but how do i prototype Inner? class Outer::Inner; but it gives me errors when i declare a pointer of type Inner later Outer::Inner * variable;
Any help much appreciated. |
Roy Underthump
Member #10,398
November 2008
![]() |
http://www.allegro.cc/forums/thread/594939 I'm not sure if it's quite the same problem I love America -- I love the rights we used to have |
oscar_spencer
Member #8,914
August 2007
|
thanks, but not quite the same problem namespace ANamespace { ...definitions } ...other code namespace ANamespace { ...definitions } this is not legal class AClass { ...definitions }; ...other code class AClass { ...definitions };
|
anonymous
Member #8025
November 2006
|
You might just make Inner a non-nested normal class. If it is inner for name control only (so that the name of Outer would be part of the class name), perhaps a namespace could be used instead. As far as I know, there isn't anything really special about nested classes, except perhaps conveying the meaning that they should only be used by the enclosing class (when declared private), so that there are no objective reasons that would force you to use one or another. |
Thomas Fjellstrom
Member #476
June 2000
![]() |
I had the same issue with a "little" C++ lib I was working on. Make the inner class a regular class with a funny name, and typedef it into the outer class if you still want to access it inside like you did before. but to be brutally honest, nested classes are useless in C++. They really don't provide anything usefull over a regular class. -- |
Kitty Cat
Member #2,815
October 2002
![]() |
Quote: but to be brutally honest, nested classes are useless in C++. They really don't provide anything usefull over a regular class. It does make it so the nested class can only be used within the proper scope of the container class. If you have a nested class inside a protected/private section, nothing outside of the container class (and maybe it's derivitives) that isn't specificly friended can use it. -- |
ReyBrujo
Moderator
January 2001
![]() |
Does that work? Never tried nested classes, makes everything harder to read in headers. -- |
Paul whoknows
Member #5,081
September 2004
![]() |
____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
ReyBrujo
Moderator
January 2001
![]() |
Yuck, awful. Looks like nested namespaces. -- |
anonymous
Member #8025
November 2006
|
The nested class should be in the private/protected section of the outer class. Then it is available only for the containing class and you'll get a compiler error should you try to use the nested class elsewhere ("class X is private in this context"). Of course, you could also use separate classes and make everything in the "nested" class private and give access to the "outer" class through friendship, only that gives "outer" access to everything, not just its public interface. |
Paul whoknows
Member #5,081
September 2004
![]() |
Quote: Yuck, awful.
I find inner classes useful when I have to implement relatively complex tasks in the methods of the outer class. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
Thomas Fjellstrom
Member #476
June 2000
![]() |
Quote: I find inner classes useful when I have to implement relatively complex tasks in the methods of the outer class. But they really don't need to be inner classes then, just a small helper class, doesn't really matter where you put it. -- |
oscar_spencer
Member #8,914
August 2007
|
Thanks for all the replies, seems the general consensus is to not use nested classes I think that it makes the interface cleaner tho, especially list<Object>::iterator it;
|
anonymous
Member #8025
November 2006
|
Don't forget about the typedefs. I'm pretty sure that in your STL implementation you'll find that the iterators are not nested types and the "nestedness" comes from typedefs like this: typedef _List_iterator<_Tp> iterator; typedef _List_const_iterator<_Tp> const_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; As you can see, the STL already contains an implementation of std::reverse_iterator that can be used to synthesize reverse iterators from any bidirectional iterator, so at least this couldn't be a nested class. |
|