Default member functions for C + + classes

Source: Internet
Author: User

The member function implies this pointer parameter:

Each member function is an implicit pointer parameter (except for constructors);

When an object calls a member function, the compiler passes the address of the object to the this pointer;

1, constructors (a public member function is required to initialize a private member variable, executed once when the object is constructed, the parameterless constructor and the constructor with the default value are the default constructors, the default constructor can only have one)

Mystring (constchar *str="")//constructor with default value {    newchar1 ];    strcpy (_str, str);

2, copy the constructor (using the same kind of object to initialize; The overload of the constructor; if not, the default copy constructor will be adjusted, and the default copy constructor will copy the class member initialization in turn)

    Mystring (const mystring& s)    {        newchar1];        strcpy (_str, s._str);    }
Mystring S1;
2 ways to call a copy constructor
Mystring S2 (S1);
Mystring s3=s2;

But why are the arguments passed by the copy constructor reference?

Using pass-through values will cause infinite recursive calls, such as the call method on the copy constructor, the passed parameter is an instance of mystring, because it is a pass-through parameter, so it will copy the formal parameter s to the argument S1 and call the copy constructor, so it will be infinitely recursive

3, destructor (when the life cycle of an object ends, the C + + compiler automatically calls the destructor; be responsible for doing some cleanup work)

    ~Mystring ()    {        if  (_str)            Delete[]_str;    }

4. Overloading of assignment operators

    operator= (const mystring& s)    {        if ( this = &s)        {            charnewchar1];             Delete [] _str;             = tmp;            strcpy (_str, s._str);        }         return *this;    }

Mystring S4;
S4 = S1;

A, declare the return value type as a reference to that type, and return a reference to the instance itself before the function ends to support continuous replication such as (S1=S2=S3); ? Why

The reference is an lvalue (which can be placed to the left of the assignment operator), the return reference can be manipulated directly , and it can be continuously assigned, so S2 can also copy the value to S1 after S3 the value to S2.

In addition, the reference as a return value has the following points: ( using a value as the return value creates a temporary variable to store the return value, because the local variable is freed when the function returns must be stored with a temporary variable, and the pointer is the same, except that the temporary variable the address of the temporary variable when the )

In the member function of the class, the referenced class object is returned, and of course it cannot be a class object defined within the function (it will be freed), typically the object to which this is directed;

Do not return a reference to a local object, and when the function finishes executing, the program frees the storage space allocated to the local object. At this point, a reference to the local object points to the indeterminate memory.

b, the passed parameter is declared as a constant reference, it is not necessary to call the copy constructor to avoid unnecessary consumption;

C, before allocating new memory to release their existing memory, in case of memory leaks;

D, judging the parameters passed in and the current instance (*this) is not the same instance, if the same instance when the memory is freed, the memory passed in the parameters is freed, and can not find the content to be copied.

Default member functions for C + + classes

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: 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.