C ++-create and access dynamic objects

Create dynamic objects

Prior to this, the syntax for creating an object was:

Class object ( arguments );
 // Class is the class name
 // object is the object
 // (arguments) are the parameters passed in, this part can be absent

 Now we want to create objects dynamically, we still need to use the keyword new. The syntax is as follows:

// no-argument constructor 
ClassName * = pObject new new ClassName; 
// no-argument constructor number can be written as:
// ClassName * = pObject new new ClassName ();
// have argument constructor ClassName * = pObject new new ClassName (arguments The );

Access dynamic objects

Another difference from the previous method is that the previous method returned an object, and now returns a pointer of type Class, this pointer points to the location of the created object. Therefore, if we want to use this object, we must first use the dereference operator (*) to get the object, and then use the dot operator (.) To manipulate the properties and methods of the object.

// string is special compared to other basic data types, string in C ++ is a class itself
 // for convenience, we use the string class directly 
#include <iostream> using namespace std; int main () 
{ // parameter construction The function string * pString = new string ( " abcdef " ); 
    cout << "The string is: " << * pString << endl; 
    cout << " The length of the string is: " << (* pString) .length ( ) << endl;
     return 0 ; 
}

 


    
      

operation result:

But in this way, it seems a bit troublesome, you must dereference the object itself before you can operate it. Therefore, C ++ provides a member selection operator ("->", a dash "-" followed by a greater than sign ">"), its role is to simplify pointer access to object members. As in the example above:

cout << " The length of the string is: " << (* pString) .length () << endl;

Can be written as:

cout << " The length of the string is: " << pString-> length () << endl;

Why create objects dynamically

Speaking of which, we haven't said why we should create objects dynamically. You might say, is it just using new? No difference from the original?

One of the mysteries lies in the keyword new. We know that the new keyword will dynamically allocate a section of memory space, which is not possible before statically created objects. In other words, the memory of statically created objects is not managed by the programmer, but is completed during the compilation process. But the dynamic is different, it is done at runtime, we can dynamically manage its memory space.

If a program creates 10,000 objects statically, then the memory may not be enough, because there is no way to destroy them. But using the dynamic method, we can delete it instantly, reducing the memory burden.

Another point is that if users want to create objects at runtime, they can only use the method of dynamically creating objects. This is similar to the memory space in which we dynamically created an array.



Guess you like

Origin www.cnblogs.com/bwjblogs/p/12725378.html