The difference between the pointer and the object classes in C ++ classes

. 1 #include <the iostream> 
 2 #include < String > 
 . 3  the using  namespace STD; 
 . 4  class Student 
 . 5  { 
 . 6  public : 
 . 7  static  int Number;  
 . 8  String name; 
 . 9  
10  public : 
 . 11  Student () {} 
 12 is  void  SET ( String STR ) 
 13 is  { 
 14 name = STR; 
 15 Number ++;     // call the static data member 
16 } 
 . 17  
18 is  void Print ()   // state member function Print () 
. 19  { 
 20 is STD :: COUT <<name << " : The Students. Of The Number of IS " <<Number << " . Numbers " <<STD :: endl; // call the static data member 
21 is  } 
 22 is  }; 
 23 is  
24  int Student Number :: = 0 ;   // static data members initialized 
25  
26 is  int main ( int argc, char ** the argv) 
 27  { 
 28 Student* s1; 
29 s1 = new Student(); 
30 s1->set("111"); 
31 
32 Student s2; 
33 s2.set("222"); 
34 
35 s1->print(); 
36 s2.print(); 
37 
38 return 0; 
39 } 

For class student, we define an object and a pointer.

Pointer Class: He is a memory address value, he points to the class object in memory storage (including some members of the variable value assigned).
Object: He is the constructor for the use of the class allocated in memory a memory (including a number of member variables the assigned value).
when the application:
1. the reference member: object with the operator; pointers "->" operator ".".
2. lifetime: if the member variable, class destructor is released to space; function if temporary variables, then the scope is a function of the body; and a pointer, the release of the allocated memory block delete the corresponding need to use place.
Note: use new new, must delete ..

Object class: using a memory stack, a local temporary variables.
Pointer class: using a memory heap, is a permanent variable unless you release it.


When the class is the base class virtual function, Func is it a virtual function, then the call Func:
object classes: call is its own Func;
pointer classes: call is assigned to it a space that class Func;

A pointer to an object class for this class and what (memory allocated by the new operator) when the difference between the application
1. classes and objects are two different things, an object instance of the class;
2. the object is allocated in the stack, the use of new generated object is allocated in the heap;
3. to play a strong role in a virtual function, the pointer must be used to access the object.

Pointers may achieve polymorphism, not directly with the object
execution definition objects in the stack space
new heap at

Note the name of the type.
One is the Student
One is Student *
Student is direct access to an object
Student * is an indirect access to an object, because through a pointer for the media.
Type determines what you can do.

In fact, the same basic role is to call the member variables and member functions of the class used
when you want to clear using this class, it is best to use an object, if you want to use C ++ dynamic binding, it is best to use a pointer or reference
pointers and references to use them more flexible, easy to implement and polymorphism

Pointer Class: He is a memory address value, he points to the class object in memory storage (including some members of the variable value assigned).
Objects, which is the constructor for the use of the class allocated a memory in the memory (including a number of member variables the assigned value).
when the application:
1. the reference member: object with the operator; pointers "->" operator ".".
2. lifetime: if the member variable, class destructor is released to space; function if temporary variables, the scope is the function pointer in vivo, need to use the allocated memory block delete released at the appropriate place.
NOTE: be sure to delete new use ..
when there is a virtual function of the class base class, f is a virtual function that is invoked when f:
object classes: call is its own Func;
pointer classes: call is assigned to the class that it Func space;

1. In the case of the class declaration has not been completed, the class can be a pointer to the statement, but not the object class declaration ...
pointer can point 2. The parent class subclasses of the object ..

When defining the object instance, allocated memory. Pointer variable is required unallocated memory class object
pointer variable is accessed indirectly, but may be multi-state (by calling the parent class subclass object pointer), and does not call the constructor.
Direct statement can be accessed directly, but can not achieve polymorphism, namely a statement calling the constructor (the allocated memory).
As for the high efficiency of the process depends on the program call may be.

One is the essence of polymorphism in C ++, only a pointer or reference can achieve polymorphism. No Object

Pointer:
a first multi-state.
Second, the function call parameters passed pointer. No matter how large your objects or structural parameters, you use the pointer, passed in the past is 4 bytes. If the object parameter passing resource consumption of too big

Guess you like

Origin www.cnblogs.com/chaoyingLi/p/11287761.html