C + + Primer plus 12th Chapter-Dynamic memory and class, copy constructor __ function

Source: Internet
Author: User
Tags function definition shallow copy static class

C + + Primer Plus chapter 12th-Dynamic memory and classes, copy constructors

1 The class declaration does not allocate storage space for the string itself, but instead uses new in the constructor to allocate space for the string. This avoids the predefined length of the string in the class declaration.


2 static members have a feature that no matter how many objects are created, the program is only creating a copy of the static variable. That is, all objects of a class share a static member, just as a phone at home can be used by all people.

Also, static member variables cannot be initialized in class declarations because the declaration describes how to allocate memory, but does not allocate memory. For static members, you can initialize a separate statement outside of a class declaration because static class members are stored separately, not as part of an object. Also note that the initialization statement identifies the type and uses the scope operator, but does not use the keyword static.


3 Note: Static data members are declared in the class declaration and initialized in the file containing the class method. The scope operator is used when initializing to indicate the class to which the static member belongs. However, if the static member is a const integer type or an enumeration type, it can be initialized in the class declaration.


4 default constructor: The compiler will provide a constructor that does not accept any arguments or perform any action (default default constructor) because the constructor is always invoked when the object is created.

Klunk::klunk () {}

Constructors with parameters can also be default constructors, as long as all parameters have default values.

Klunk (int n=0) {klunk_ct = n;}

However, there can be only one default constructor.


5 copy constructor: Used to copy an object to a newly created object. That is, it is used during initialization, including passing parameters by value, rather than in the normal assignment process. The prototype for the copy constructor of the class is as follows:

Class_name (const class_name &);

It accepts a constant reference to a class object as an argument.

Stringbad (const Stringbad &);


6 for the copy constructor, you need to know two points: when to call and what the effect is.


7 When you create a new object and initialize it to a homogeneous existing object, the copy constructor is invoked. The most common scenario is to explicitly initialize the new object to an existing object.

Stringbad motto; Motto as Stringbad object

Stringbad ditto (motto); New object, initialized to motto

Stringbad Metto = motto; New object, initialized to motto

Stringbad also = Stringbad (motto); New object, initialized to motto

Stringbad *pstringbad = new Stringbad (motto); Creates a new pointer to an object, pointing to the address of motto memory


The above 4 declarations will invoke the copy constructor Stringbad (const Stringbad &);


8 The compiler will use the copy constructor whenever the program generates a copy of the object. Specifically, the copy constructor is used when a function passes an object or function by value to return an object. Passing by value means creating a copy of the original variable. The copy constructor is also used when the compiler generates a temporary object.

Because passing objects by value invokes the copy constructor, the object should be passed by reference, saving the time to invoke the constructor and the space to store the new object.


9 The function of the default copy constructor: Copy non-static members individually (member replication is also known as shallow replication), and copy the values of members. If the member itself is a class object, the copy constructor of the class is used to copy the member object. A static function is not affected because it belongs to the entire class, not to the individual objects.


10 the solution to the problem of trying to release the memory two times in a class is to make a deep copy. That is, the copy constructor should copy the string and assign the address of the replica to the new memory member, not just the string address. So that each object has its own string, rather than a string that references another object. When a destructor is invoked, a different string is freed without attempting to release the string that has been freed.


11) Warning: If a class contains pointer members initialized with new, you should define a copy constructor to copy the pointing data, rather than the pointer, which is called a deep copy. Another form of replication (member copy or shallow copy) is just a copy of the pointer value. A shallow copy is simply a shallow copy of the pointer information and does not drill down to duplicate the structure of the pointer reference.


12 Assignment Operator: Class object assignment, implemented by automatically overloading the assignment operator for the class.

Class_name & class_name:: operator= (const class_name &);

It accepts and returns a reference to the class object.

When you assign an existing object to another object, the overloaded assignment operator is used.

Stringbad headline1 ("Celery stalks at midnight");

Stringbad knot;

knot = headline1;


13 Solve the assignment problem:

Because the target object might reference previously allocated data, the function should use delete[to release the data.

A function should avoid assigning an object to itself, otherwise, releasing the memory operation may delete the object's contents before it is assigned a value.

function returns a reference to an object.

Stringbad & stringbad::operator= (const Stringbad & ST)

{

if (this==&st)//Avoid the object assigned to itself

return *this;

delete [] str; Freeing old memory data

len = St.len; //

str= new Char [len+1]; Get a new memory string

std::strcpy (STR,ST.STR); Copy string

return *this;

}


14 NULL pointer: The literal 0 has two meanings: it can represent a numeric value of 0, or it can represent a null pointer. Some programmers use (void *) 0来 to identify null pointers (internal representations of the null pointer may not be 0), or NULL, a C-language macro that represents a null pointer. C++11 introduced a new keyword, nullptr, to represent a null pointer.

Str=nullptr;


15 Static member function: You can declare a member function as static (the function declaration must contain the keyword static, but if the function definition is separate, it cannot contain the keyword static).

First, static member functions cannot be invoked through objects, and in fact, static member functions cannot even use the this pointer. If a static member function is declared in the public part, it can be invoked using the class name and the scope resolution operator.

Second, because static member functions are not associated with a particular object, only static data members can be used. You can also use static member functions to set class-level tags to control the behavior of certain class interfaces.


16)

Related Article

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.