C + + Primer Plus 12.6 Review the various (class and dynamic memory allocation) Technical notes

Source: Internet
Author: User

12.6.1 Overloading << Operators
To redefine the << operator so that it is used with cout to display the contents of an object, define the following friend operator function:
Ostream & operator<< (ostream & OS, const C_name & obj)
{
Os << ...; Display object Contents
return OS;
}
Where C_name is the class name. If the class provides a public method that can return the required content, you can use these methods in the operator function so that they do not have to be set as friend functions.

12.6.2 conversion function
To convert a single value to a class type, you need to create a class constructor that looks like the following:
C_name (type_name value);
Where C_name is the class name, Type_name is the name of the type to be converted.
To convert a class to another type, you need to create a class member function that looks like this:
operator Type_name ();
Although the Chinese slang does not declare a return type, it should return a value of the desired type.
Be careful when using conversion functions. You can use the keyword explicit when declaring a constructor to prevent it from being used for implicit conversions.

12.6.3 whose constructors use the new class
If a class uses the new operator to allocate memory pointed to by a class member, some precautions should be taken at design time (these precautions are summarized above, which should be kept in mind because the compiler is unaware of these rules and therefore cannot discover errors).
* For all Class members assigned by new when pointing to memory, delete should be used in the class's destructor, which frees the allocated memory.
* If the destructor frees memory by using delete on pointer class members, each constructor should use new to initialize the pointer, or set it to a null pointer.
* You should define a copy constructor that allocates memory instead of pointing the pointer to an existing memory. This will enable the program to initialize the class object to another class object. The prototype of this early function of the dog is usually as follows:
ClassName (const ClassName &)
You should define a class member function that overloads the assignment operator, whose function is defined as follows (where C_pointer is a class member of C_name, and the type points to the type_name pointer). The following example assumes the use of new[] to initialize the variable c_pointer:
C_name & c_name::operator= (const C_name & CN)
{
if (this = = & cn)
return this; Done if self-assignment
delete [] c_pointer;
Set size number of type_name units to be copied
C_pointer = new Type_name[size];
Then copy data pointed through Cn.c_pointer to
Location pointed to by C_pointer
...
return *this;
}

C + + Primer Plus 12.6 Review the various (class and dynamic memory allocation) Technical notes

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.