<p>I am working on a collection of classes used for video playback and recording. I have one main class which acts like the public interface, with methods like <code>play()</code>, <code>stop()</code>, <code>pause()</code>, <code>record()</code> etc... Then I have workhorse classes which do the video decoding and video encoding. </p>

<p>I just learned about the existence of nested classes in C++, and I'm curious to know what programmers think about using them. I am a little wary and not really sure what the benefits/drawbacks are, but they seem (according to the book I'm reading) to be used in cases such as mine.</p>

<p>The book suggests that in a scenario like mine, a good solution would be to nest the workhorse classes inside the interface class, so there are no separate files for classes the client is not meant to use, and to avoid any possible naming conflicts? I don't know about these justifications. Nested classes are a new concept to me. Just want to see what programmers think about the issue.</p>

[{'answer_id': 332, 'author': 'saint_groceon', 'author_id': 59, 'author_profile': 'https://Stackoverflow.com/users/59', 'pm_score': 6, 'selected': True, 'text': '<p>I would be a bit reluctant to use nested classes here.  What if you created an abstract base class for a "multimedia driver" to handle the back-end stuff (workhorse), and a separate class for the front-end work?  The front-end class could take a pointer/reference to an implemented driver class (for the appropriate media type and situation) and perform the abstract operations on the workhorse structure.</p>\n\n<p>My philosophy would be to go ahead and make both structures accessible to the client in a polished way, just under the assumption they would be used in tandem.</p>\n\n<p>I would reference something like a <a href="http://qt-project.org/doc/qt-4.8/qtextdocument.html" rel="noreferrer">QTextDocument</a> in Qt.  You provide a direct interface to the bare metal data handling, but pass the authority along to an object like a QTextEdit to do the manipulation.</p>\n'}, {'answer_id': 2019, 'author': 'graham.reeds', 'author_id': 342, 'author_profile': 'https://Stackoverflow.com/users/342', 'pm_score': 3, 'selected': False, 'text': "<p>One way of deciding whether or not to use nested classes is to think whether or not this class plays a supporting role or it's own part.</p>\n\n<p>If it exists solely for the purpose of helping another class then I generally make it a nested class. There are a whole load of caveats to that, some of which seem contradictory but it all comes down to experience and gut-feeling.</p>\n"}, {'answer_id': 2023, 'author': 'DShook', 'author_id': 370, 'author_profile': 'https://Stackoverflow.com/users/370', 'pm_score': 2, 'selected': False, 'text': '<p>sounds like a case where you could use the <a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow noreferrer">strategy pattern</a></p>\n'}, {'answer_id': 70727, 'author': '0124816', 'author_id': 11521, 'author_profile': 'https://Stackoverflow.com/users/11521', 'pm_score': 2, 'selected': False, 'text': "<p>Sometimes it's appropriate to hide the implementation classes from the user -- in these cases it's better to put them in an foo_internal.h than inside the public class definition. That way, readers of your foo.h will not see what you'd prefer they not be troubled with, but you can still write tests against each of the concrete implementations of your interface.</p>\n"}, {'answer_id': 82288, 'author': 'Frederik Slijkerman', 'author_id': 12416, 'author_profile': 'https://Stackoverflow.com/users/12416', 'pm_score': 3, 'selected': False, 'text': "<p>You would use a nested class to create a (small) helper class that's required to implement the main class. Or for example, to define an interface (a class with abstract methods).</p>\n\n<p>In this case, the main disadvantage of nested classes is that this makes it harder to re-use them. Perhaps you'd like to use your VideoDecoder class in another project. If you make it a nested class of VideoPlayer, you can't do this in an elegant way.</p>\n\n<p>Instead, put the other classes in separate .h/.cpp files, which you can then use in your VideoPlayer class. The client of VideoPlayer now only needs to include the file that declares VideoPlayer, and still doesn't need to know about how you implemented it.</p>\n"}, {'answer_id': 93509, 'author': 'luke', 'author_id': 16434, 'author_profile': 'https://Stackoverflow.com/users/16434', 'pm_score': 2, 'selected': False, 'text': '<p>You should use an inner class only when you cannot implement it as a separate class using the would-be outer class\' public interface.  Inner classes increase the size, complexity, and responsibility of a class so they should be used sparingly.</p>\n\n<p>Your encoder/decoder class sounds like it better fits the <a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow noreferrer">Strategy Pattern</a></p>\n'}, {'answer_id': 107530, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>One reason to avoid nested classes is if you ever intend to wrap the code with swig (<a href="http://www.swig.org" rel="nofollow noreferrer">http://www.swig.org</a>) for use with other languages. Swig currently has problems with nested classes, so interfacing with libraries that expose any nested classes becomes a real pain.</p>\n'}, {'answer_id': 109887, 'author': 'Allan Wind', 'author_id': 9706, 'author_profile': 'https://Stackoverflow.com/users/9706', 'pm_score': 2, 'selected': False, 'text': '<p>We hit an issue with a semi-old Sun C++ compiler and visibility of nested classes which behavior changed in the standard.  This is not a reason to not do your nested class, of course, just something to be aware of if you plan on compiling your software on lots of platforms including old compilers.</p>\n'}, {'answer_id': 123101, 'author': 'Michael Mathews', 'author_id': 21242, 'author_profile': 'https://Stackoverflow.com/users/21242', 'pm_score': 1, 'selected': False, 'text': '<p>Another thing to keep in mind is whether you ever envision different implementations of your work functions (such as decoding and encoding). In that case, you would definitely want an abstract base class with different concrete classes which implement the functions. It would not really be appropriate to nest a separate subclass for each type of implementation.</p>\n'}, {'answer_id': 136881, 'author': 'Carl', 'author_id': 13760, 'author_profile': 'https://Stackoverflow.com/users/13760', 'pm_score': 2, 'selected': False, 'text': '<p>Well, if you use pointers to your workhorse classes in your Interface class and don\'t expose them as parameters or return types in your interface methods, you will not need to include the definitions for those work horses in your interface header file (you just forward declare them instead). That way, users of your interface will not need to know about the classes in the background. </p>\n\n<p>You definitely don\'t need to nest classes for this. In fact, separate class files will actually make your code a lot more readable and easier to manage as your project grows. it will also help you later on if you need to subclass (say for different content/codec types).</p>\n\n<p>Here\'s more information on the <a href="http://www.apibook.com/Chapter_3.pdf" rel="nofollow noreferrer">PIMPL pattern</a> (section 3.1.1).</p>\n'}]
['https://Stackoverflow.com/questions/330', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/63/']