Pros and cons of using nested C++ classes and enumerations?

前端 未结 13 2194
悲&欢浪女
悲&欢浪女 2020-11-29 23:43

What are the pros and cons of using nested public C++ classes and enumerations? For example, suppose you have a class called printer, and this class also store

13条回答
  •  悲哀的现实
    2020-11-30 00:30

    paercebal said everything I would say about nested enums.

    WRT nested classes, my common and almost sole use case for them is when I have a class which is manipulating a specific type of resource, and I need a data class which represents something specific to that resource. In your case, output_tray might be a good example, but I don't generally use nested classes if the class is going to have any methods which are going to be called from outside the containing class, or is more than primarily a data class. I generally also don't nest data classes unless the contained class is not ever directly referenced outside the containing class.

    So, for example, if I had a printer_manipulator class, it might have a contained class for printer manipulation errors, but printer itself would be a non-contained class.

    Hope this helps. :)

提交回复
热议问题