Java value assignment operator, copy initialization, and this pointer

Source: Internet
Author: User
Tags constructor

 

1) first, the difference between what is a value assignment and what is initialization.

2) the overload in the program uses reference transfer. The reason is: ① as we all know, parameters passed with values will generate a copy in the function to be passed, no exception. If this object is large, the copy will waste a lot of space. ② In some cases, you may want to record the number of objects. If the compiler generates an additional object each time when using the value assignment operator, it is possible to see more objects than medical. The transfer of references helps avoid creating too many objects.

3) return alpha (data); the return value is a copy of the object where the overload function is located, rather than the same object. The returned value concatenates operator = a3 = a2 = a1;

4) However, the returned value of alpha operator = (alpha & a) has the same disadvantage as the value parameter: wasting memory space. But can I use a reference to return the function value such as alpha & operator = (alpha &?

The answer is no: the non-static local variables created in the function are destroyed when the function returns. The value returned by the reference is only the actual address that you want to return, is meaningless, and even serious errors may occur. (Here we can use the this pointer to solve this problem, which will be explained later)

Note: The value assignment operator is the only one that cannot be inherited. If the value assignment operator is overloaded in the base class, the same function cannot be reloaded in any derived class.

 

The code is as follows: Copy code

# Include <iostream>
Using namespace std;
Class alpha
{
Public:
Alpha (): data (0) {}// constructor without parameters
Alpha (int d): data (d) {}// constructor of a parameter
Void diplay () // Display Data
 {
Cout <data <endl;
 }
Alpha (alpha & a) // reload copy constructor
 {
Data = a. data;
Cout <"copy constructor invoked! "<Endl;
 }
Alpha operator = (alpha & a) // overload value assignment operator
 {
Data = a. data;
Cout <"assignment operator invoked! "<Endl;
Return alpha (data); // single-parameter constructor
 }
Private:
Int data;
};
Int main ()
{
Alpha a1 (32 );

Alpha a2; // non-parameter constructor
A2 = a1; // value assignment operator
A2.diplay ();

Alpha a3 = a1; // Copy the constructor
A3.diplay ();

Alpha a4 (a1); // Copy the constructor
A4.diplay ();

Return 0;
}


. Look at several situations of the function

1) function parameters

View sourceprint? Void func (alpha); // passing objects with values

Func (a1); // function call

At this time, the copy constructor will be called to create a copy of the object a1 and hand over the copy to the function func () operation. (Of course, the reference or pointer won't call the copy constructor)

2) function return value

View sourceprint? Alpha func () // function declaration

A2 = func () // function call

Here: first, the program calls the copy constructor to wear a copy of the return value of func;

Then, this value is assigned to a2 (call the value assignment operator ).

3) The Copy constructor is alpha (alpha & a). Why is it not in alpha (alpha a) format?

A: The copy constructor must use references. Otherwise, memory overflow is reported. (Because if the parameter is passed with a value, you need to create a copy of the value. How to create a copy? The copy constructor is used, but the original function is to define the copy constructor, so that you can call the constructor continuously. What do you mean !~)

2. this pointer

Every member function of an object can access a magic pointer, that is, the this pointer to the object itself. Therefore, the address of the object itself can be found in any object.

The code is as follows: Copy code

# Include <iostream>
Using namespace std;
Class alpha
{
Public:
Alpha (): data (0 ){}
Alpha (int d): data (d ){}
Void display ()
 {
Cout <data <endl
<This-> data <endl;
 }
// Alpha operator = (alpha & a) // overload the value assignment operator
//{
// Data = a. data;
// Cout <"assignment operator invoked! "<Endl;
// Return alpha (data); // single-parameter constructor
//}
Alpha & operator = (alpha & a) // overload value assignment operator
 {
Data = a. data;
Cout <"assignment operator invoked! "<Endl;
Return * this; // return value through this pointer
 }
Private:
Int data;
};
Int main ()
{
Alpha a1 (37 );
A1.display (); // The output result of this-> data is the same.
Alpha a2, a3;
A3 = a2 = a1; // call the value assignment operator
Cout <"a2 ="; a2.display ();
Cout <"a3 ="; a3.display ();
Return 0;
}

Note: In the instance, use the this pointer to return the value. (From the returned values of member functions and overload operators, this pointer is a more practical usage)

1) this pointer points to the object to which the member function belongs, so * this is the object itself. Generally, the utility reference and this pointer return data from the overload value assignment operator to avoid creating additional objects.

2) Note: this pointer is invalid in the static member function, because the static member function does not belong to any specific object.

III. dynamic_cast and typeid

These two functions are usually used when many classes are derived from a base class. To enable dynamic type conversion to work, the base class must be polymorphism (that is, at least one virtual function is included ).

1. dynamic_cast can change the pointer type

The code is as follows: Copy code

# Include <iostream>
# Include <typeinfo>
Using namespace std;
Class Base
{
Public:
Base (){}
Virtual void vertfunc () // to use dynamic_cast, the base class must be of a polymorphism type
{}
Base (int B): ba (B ){}
Void show ()
 {
Cout <"Base: ba =" <ba <endl;
 }
Protected:
Int ba;
};
Class derv1: public Base
{};
Class derv2: public Base
{
Public:
Derv2 (): Base (){}
Derv2 (int B, int d): Base (B), da (d ){}
Void show ()
 {
Cout <"derv2: ba =" <ba <"da =" <da <endl;
 }
Private:
Int da;
};
Bool isDerv1 (Base * pUnknown)
{
Derv1 * pderv1;
If (pderv1 = dynamic_cast <derv1 *> (pUnknown ))
Return true;
Else
Return false;
}
Int main ()
{
Derv1 * d1 = new derv1;
Derv2 * d2 = new derv2;
If (isDerv1 (d1 ))
Cout <"d1 is an object of the derv1 class" <endl;
Else
Cout <"d1 is not an object of the derv1 class" <endl;
If (isDerv1 (d2 ))
Cout <"d2 is an object of the derv1 class" <endl;
Else
Cout <"d2 is not an object of the derv1 class" <endl;

Base * pbase = new Base (10 );
Derv2 * pderv = new derv2 (22, 33 );
Pbase-> show (); pbase = dynamic_cast <Base *> (pderv); pbase-> show (); // derived class to the Base class
Pbase = new derv2 (34,32 );
Pderv-> show (); pderv = dynamic_cast <derv2 *> (pbase); pderv-> show (); // base class to derived class
Return 0;
}

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.