Differences between class objects and class pointers

Source: Internet
Author: User
Differences between class objects and class pointers zz

The following procedure:

# Include <iostream>
# Include <string>
Using namespace std;

Class Student
{
Public:
Static int number;
String name;

Public:
Student (){}

Void set (string str)
{
Name = str;
Number ++; // call a static data member
}

Void print () // State member function print ()
{
Std: cout <name <": The number of the students is" <number <"numbers." <std: endl; // call a static data member
}
};

Int Student: number = 0; // static data member Initialization

Int main (int argc, char ** argv)
{
Student * s1;
S1 = new Student ();
S1-> set ("111 ");

Student s2;
S2.set ("222 ");

S1-> print ();
S2.print ();

Return 0;
}

For student classes, an object and a pointer are defined.

Class pointer: it is a memory address value that points to class objects stored in the memory (including values assigned by some member variables ).
The class constructor is used to allocate a piece of memory in the memory (including the values assigned by some member variables ).
Application Time:
1. Reference members: the object uses the "." operator; the pointer uses the "->" operator.
2. life cycle: if it is a member variable, it is the class destructor to release space; if it is a temporary variable in the function, the scope is the function body. the pointer needs to use delete to release the allocated memory block in the corresponding place.
Note: to use new, you must delete ..

Class Object: uses the memory stack and is a local temporary variable.
Class pointer: uses a memory heap, which is a permanent variable unless you release it.

When a class is a base class with virtual functions and Func is a virtual function, call Func:
Class Object: calls its own Func;
Class pointer: calls the Func class that is assigned to it space;

What are the differences between a class object and the class pointer (memory allocation using the new operator) in the application?
1. Classes and objects are two different things. objects are class instances;
2. objects are allocated in the stack, and objects generated using new are allocated in the heap;
3. to make full use of virtual functions, you must use pointers to access objects.

Pointers can achieve polymorphism, but objects cannot be used directly.
Execute the definition object in the stack space
New in heap

The type determines what you can do.

In fact, basically the same function is used to call the member variables and member functions of the class.
When you want to explicitly use this class, it is best to use objects. If you want to use dynamic binding in C ++, it is best to use pointers or references.
Pointers and references are more flexible to use and can easily implement polymorphism.

1. When the class declaration has not been completed, you can declare a pointer to the class, but cannot declare the class object...
2. the pointer to the parent class can point to the subclass object ..

Memory is allocated when an object instance is defined. Pointer variables do not allocate the memory required for class objects unless new

The pointer variable is indirectly accessed, but can realize polymorphism (The subclass object can be called through the parent class pointer) without calling the constructor.
Direct declaration can be accessed directly, but cannot implement polymorphism. The declaration calls the constructor (allocated memory ).
The high efficiency depends on the program call process.

One of the essence of C ++ is polymorphism. Only pointers or references can achieve polymorphism. No object

Pointer:
First, implement polymorphism.
Second, in the function call, pass the pointer parameter. No matter how large your object or structure parameters are, you can use a pointer to transmit four bytes in the past. If an object is used, passing parameters will occupy too much resources.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.