class printer
{< br />public:
std::string name_;
enum TYPE
{
TYPE_LOCAL,
TYPE_NETWORK,
};
class output_tray
{
...
};
...
};
printer prn;
printer::TYPE type;
printer::output_tray tray;
Or:
class printer
{
public:
std::string name_;
...
};
enum PRINTER_TYPE
{
PRINTER_TYPE_LOCAL,
PRINTER_TYPE_NETWORK,
};
class output_tray
{
...
};
printer prn ;
PRINTER_TYPE type;
output_tray tray;
I can see the benefits of nested private enumerations/courses, but when it comes to public places/offices, the offices are separated – It seems more like a style choice.
So, which one do you prefer? Why?
Class nested in class There are a few side effects that I usually consider to be defects (if not pure anti-patterns).
Let us imagine the following code:
class A
{
public :
class B {/* etc. */} ;
// etc.
} ;
Even :
class A
{
public :
class B ;
// etc.
} ;
class A::B
{
public :
// etc.
} ;
< p>So:
> Privilegied Access: A:: B has privileged access to all members of A (methods, variables, symbols, etc.), which weakens the package
> The scope of A is a symbol Search candidates: The code from B will see all symbols from A as possible candidates for symbol search, which may confuse the code.
> forward-declaration: If you don’t give a complete A declaration, you can’t Forwarding A::B.
>Scalability: Unless you are the owner of A, you cannot add another class A::C
>Code detail level: Putting the class into the class will only make the title more Big. You can still divide it into multiple declarations, but there is no way to use namespace-like aliases, import or use.
As a conclusion, unless an exception is made (e.g. a nested class is a nested class The private part… Even so…), I don’t think the nested classes in ordinary code make any sense, because the flaws exceed the advantages of size.
Furthermore, it is a bit clumsy trying to simulate namespaces without Use the C namespace.
In terms of pro, you isolate this code. If it is private, then it cannot be used, but it comes from the “external” class…
Nesting Enumeration
Pros: Everything.
Scam: Nothing.
The fact is that enumeration items will pollute the world:
p>
// collision
enum Value {empty = 7, undefined, defined} ;
enum Glass {empty = 42, half, full} ;
// empty is from Value or Glass?
By putting each enumeration in a different namespace/class, this conflict can be avoided:
namespace Value {enum type {empty = 7, undefined, defined}; }
namespace Glass {enum type {empty = 42, half, full}; }
// Value::type e = Value::empty;
// Glass::type f = Glass::empty ;
Note that C 0x defines class enumeration:
enum class Value {empty, undefined, defined} ;
enum class Glass {empty, half, full} ;
// Value e = Value::empty ;
// Glass f = Glass::empty ;
It is for this kind of problem.
What are the advantages and disadvantages of using nested public C classes and enumerations ? For example, suppose you have a class called printer, which also stores information about the output tray, you can:
class printer
{< br />public:
std::string name_;
enum TYPE
{
TYPE_LOCAL,
TYPE_NETWORK,
};
class output_tray
{
...
};
...
};
printer prn;
printer::TYPE type;
printer::output_tray tray;
Or:
class printer
{
public:
std::string name_;
...
};
enum PRINTER_TYPE
{
PRINTER_TYPE_LOCAL,
PRINTER_TYPE_NETWORK,
};
class output_tray
{
...
};
printer prn ;
PRINTER_TYPE type;
output_tray tray;
I can see the benefits of nested private enumerations/courses, but when it comes to public places/offices, the offices are separated – It seems more like a style choice.
So, which one do you prefer? Why?
Nested classes
Classes nested in classes have several side effects, which I usually think are defects (if not Pure anti-pattern).
Let us imagine the following code:
class A
{
public :
class B {/* etc. */} ;
// etc.
} ;
Even:
class A
{
public :
class B ;
// etc.
} ;
class A::B
{
public :
// etc.
} ;
So:
> Privilegied Access: A::B has privileged access to all members of A (methods, variables, symbols, etc.), which weakens the package
> The scope of A is a candidate for symbol search: the code from B’s internal will look To all the symbols from A as possible candidates for symbol search, this may confuse the code
> forward-declaration: If the complete A declaration is not given, it cannot be forwarded A::B.
>Expandable Sex: Unless you are the owner of A, you cannot add another A::C class
>Code detail level: Putting the class into the class will only make the title bigger. You can still divide it into multiple declarations, But there is no way to use namespace-like aliases, import or use.
As a conclusion, except for exceptions (e.g. nested class is a private part of nested class… Even so…), I think Nested classes in normal code don’t make any sense, because the flaws exceed the advantages of size.
In addition, it is a bit clumsy trying to simulate namespaces without using C namespaces.
In terms of pro, you isolate this code. If it is private, then it cannot be used, but it comes from the “external” class…
Nested enumeration
Advantages: everything .
Scam: nothing.
The fact is that the enumeration will pollute the world:
// collision
enum Value {empty = 7, undefined, defined} ;
en um Glass {empty = 42, half, full} ;
// empty is from Value or Glass?
By placing each enum in a different namespace/class This conflict can be avoided:
namespace Value {enum type {empty = 7, undefined, defined }; }
namespace Glass {enum type {empty = 42, half, full}; }
// Value::type e = Value::empty ;
// Glass::type f = Glass::empty ;
Note that C 0x defines class enumeration:
enum class Value {empty, undefined, defined} ;
enum class Glass {empty, half, full } ;
// Value e = Value::empty ;
// Glass f = Glass::empty ;
It is precisely because of this problem.< /p>