For function interface design, there are three basic rules here:
Never return reference or pointer to a local object in afunction.
Never return a dereferenced point to a local new object.
Never return reference or pointer to a private orprotected member data.
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 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.
pretect keyword don’t use very often, it just used in inheritance context,
you should use private keyword first if you real want to have good
Encapsulation
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.
String& operator=(const char*a); is 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 "«"