Nesting a class does not create a class member of another class. Instead,
it defines a type that is known just locally to the class that contains
the nested class declaration. A good example is Class queue nest class
node, because node is just used inside the class Queue.
Virtual function must be member, operator>> and << are never be
members, or It maybe be a friend. Only non-member functions get type
conversions on their left-most argument. In the previous example, If
you want to use support 2* obj, You need make operator * to be non
member function. Detail can be seen in effective c++.
Keep in mind that only a class declaration can decide which functions
are friends, so the class declaration still controls which functions access
private data.
Protect keyword don’t use very often, it is just used in inheritance
context. Child class can access base class protected member. you should
use private keyword first if you real want to have good Encapsulation
and Never return reference or pointer to a private or protectedmember data.
In C++ primer p653, you can see a good example class interface. You
should remember it as a basic pattern. If you use new allocate memory
inside of your class, you should define: copy ctor, assignment operator
and destructor, move copy ctor, and move assignment.
You need to declare operator << inside of namespace outside of
class
If you don’t use smart pointer and allocate use new. you should
follow five rules.( including move ctor and move assignment) if you
class includes a resource.
Member function can access all the instance private data, such as
other.m_str, and no semicolon after each function.
String& operator=(const char*a); is an option, why it make str=temp more
efficient, see C++ Primer P652
Friend has three categories:
Friend Class: just as TV and RemoteControl, you can declare
RemoteControl a friend class inside TV.
Friend Member functions: You can select some member functions
to be friend of another class, In this way, you need forward
declaration. When you write class TV; It doesn’t define a TV class,
it just tell compile, TV is a class, definition can be done later.
Common Friend method, a good example is overload operator "«"
Prefer minimal classes to monolithic classes: big class is difficult to reach
error-safe because it tackle multiple responsibilities. It’s also difficult
maintain, understand and deploy.
Interface Principle: For a class X, all functions, including free functions, that
both 1) "Mention" X 2) Are "supplied with" X are logically part of X,
because they form part of the interface of X. In this definition, 1) Cal1
Mention X, and 2) Cal1 in the namespace MAT, so caller can use
ADL(argument depend lookup) loop Cal1 in namespace MAT, so
Cal1 is an interface of class Matrix, even it’s not a memberfunction of it. Detail can be see in "exceptional C++ item31to
item34.
Prefer writing nonmember nonfriend functions
operator =, ->, [], () must be members
needs a different type as its left-hand arguent, such as operator «,
use nonmember
leftmost argument needs type conversion, use nonmember
can be implement using the class public interface alone, use
nonmember.