views:

155

answers:

4

my question is how often do You really use nested classes in Your practice and in which cases? what is the real power of the nested classes, what can't be done without them? P.S. please don't explain what is it, I know it (from technical point of view)

+6  A: 

I usually use nested classes to embed finders objects (to use with std::find_if) into my specific types.

Something like:

// Dummy example
class Foo
{
  public:
    class finder
    {
      public:

        finder(int value) : m_value(value) {};

        bool operator()(const Foo& foo) { return (foo.m_int_value == value); }

      private:

        int m_value;
    };

   private:

     int m_int_value;

     friend class finder;
};

Then:

vector<Foo> foo_list;
vector<Foo>::iterator foo = 
  std::find_if(foo_list.begin(), foo_list.end(), Foo::finder(4));

This could of course be done without the use of nested classes. But I find it quite elegant because the finder has no use outside of the class definition:

If I ever delete the class in case of code-refactoring the finder should be deleted as well.

ereOn
I think the right way is to use function objects, like the `std::mem_fun` `std::ptr_fun` or even `boost::bind`
Yogesh Arora
@Yogesh: `Foo::finder` is also a function object. For more complicated tests, this can be more readable than `boost::bind` or similar.
Bart van Ingen Schenau
@Yogesh Arora: This was, of course, a simplified example and I myself use `boost::find` and `std::mem_fun` a lot. But for some complex cases, I believe using a finder object makes my code easier to read and maintain. I understand your point, but does my answer really deserves a down-vote ? It's not like what I suggested was fundamentally wrong.
ereOn
I dont see a reason why these have to be nested classes, these could just also be small classes which could be declared in cpp files, hidden from what the client sees in the header files.
Yogesh Arora
@Yohesh Arora: Thanks for removing the down-vote. I'm fine with the client seeing this finder class. Actually, I even **want** him to see it so he can use it whenever he needs to. My example is over-simplified: In a real case scenario, the implementation logic of the finder class would be hidden in the `.cpp` file. Nesting it strenghten encapsulation.
ereOn
By the way, it's not necessarily needed to declare the class inside another class... if you want to be able to add '::finder' to your class, you can define the class globally and use a typedef inside your class. I do this to have a '::iterator' for some of my custom collections.
Ken Simon
A: 

They're useful when a class needs to contain grouped/encapsulated data. You can devise it all into a private nested class to prevent unintentional use of the data type by the outside world.

It can also be used as a possibly clearer way to accomplish friendship. By nesting the class instead of using friendship you further encapsulate the friend relationship between the code parts.

Mark B
+1  A: 

I use them rarely for the following reasons:

  • They tend to be implementation details which I don't like to have in the header file.
  • I don't like the syntax.

Sometimes I use embedded structs (for POD, no methods).

ur
A: 

I use them as utility classes that need to access my class: signal handlers, timers, especially when using pimpl, so the implementation is already in a cpp-file.
There is nothing in C++ that can't be done without nested classes, but it is a nice way to scope-limit these classes that mostly just dispatch events to my class.

stefaanv