Nested classes

http://stackoverflow.com/a/14607402/1959975

Do you think you will use nested classes more often for this? This certainly makes a lot of things easier for my code.
Nope. I only nest simple classes leaning towards the POD side of things.
Huh, I've got some complex relationships that are simplified by this (mainly it means I don't have to use friend declarations any longer).
I'm with helios on this one. I've found that when I'm nesting a class that is anything but simple, I'm usually doing something wrong and need to rethink the structure of my program.
The same result could be achieved by use of friend class declaration. So there is little actual use of nesting classes. And I think they looks a little messy.
Nested classes are used to encapsulate pure implementation-support types. This is relatively unimportant in amateur software; in a large production code base, proliferation of unencapsulated implementation types is something one would want to avoid. The classic example being the fully insulated concrete class idiom.
1
2
3
4
5
6
7
8
9
10
#include <memory>

struct A
{
    A() ;
    // ....
    private:
        struct implementation ;
        std::unique_ptr<implementation> impl ;
};


If a pure implementation-support-type is not a nested non-public type, it should be placed in a nested namespace with a tell-tale name mandated by the style-guide.

1
2
3
4
5
6
7
8
9
10
11
12
namespace A_stuff
{
    namespace detail { struct implementation ; }

    struct A
    {
        A() ;
        // ....
        private:
            std::unique_ptr<detail::implementation> impl ;
    };
}


This is obviously even more important for types which are compile-time polymorphic (or where inline access from client code is required for performance reasons); ie. where a compilation firewall is (as in the fully insulated concrete class) not a possibility.
Topic archived. No new replies allowed.