[C++_g_class] Constructors for classes in C + + __jquery

Source: Internet
Author: User
Tags memory usage
First look at a topic:
There is a class
Class A
{
Public
A (); C1
A (const a&); C2
A (int i); C3
a& operator= (const a&); C4
};
Ask which constructors are called by the following statement.

A A; S1
A B=a; S2
B=a; S3
A c (); S4
A B (a); S5
A () = 5; S6

Let's talk a little bit about the harvest:

11 classes typically provide 3 constructors, default constructors; Copy constructors; Assignment function (operator= overload).
If you do not define these 3 functions, the compiler will also generate the default constructor; Copy constructors. (The assignment function is not sure whether the default will be generated).

2 Copy constructors and assignment functions, by default, a bit copy is implemented.
A class may contain pointers to a memory space, which is the primary reason for defining these two functions. You need to decide whether you want the pointer to point to the same space, or allocate memory again.

3 You must not use parentheses when using the default constructor.
such as S1, S4. S1 is obviously called C1. What about S4. S4 is a function that declares a return value of a.

4 The difference between the copy constructor and the assignment function, the copy constructor is used when the object is established; and the assignment function is referenced (nonsense:) when it is assigned a value
For example S2 is called C2. S3 is called C4. If none of them is defined, the corresponding call will use a bit copy.
The syntax in the S2 may be confusing to S3, and the syntax in S5 is quite clear.

5 If the class defines a constructor that contains a parameter of type T, then there is a type conversion of T to the class.
such as S6. 5 is of type int, Class A has the constructor of such a parameter, then 5 uses the corresponding constructor (a (int)) to convert to a object, or to generate a object of a. To the left of the equal sign, it is obvious that the default constructor is invoked to generate an object of a class. Finally, the assignment function is called and the assignment is completed.

6 also have to talk about the difference between the compiler.
A B ()
{
A A;
return A;
}

A c=b ();

Under g++, the above code does not invoke a copy constructor or a default constructor.
Using VC, the copy constructor is invoked.
I personally also think that a copy constructor should be invoked, first by generating a temporary variable returned by a local variable, and then by constructing a new object from the return value. Need to call two times. But here the compiler g++ is optimized.
If you change the value of a member variable i in a copy function, the code above will not succeed.
For example, I default constructs 0, the copy constructor modifies it to 2, and then passes the above code, under g++ C.I is still 0 instead of 2.



The experimental code is as follows:
Platform
g++ version 4.0.3
Ubuntu 6.06 lts-the Dapper drake-released in June 2006

#include <cstdio>
using namespace Std;

/*
The role of copy constructors and = number overloads is to
Prevents bit copying from the pointer in the class, and then the effect of memory usage
A destructor should reasonably release memory

= number overload in VC is required to have a return value.
= number overload Remember to determine whether it is a self assignment.
The = number overload is best returned to a&. This improves efficiency.
*/
Class A
{
Variables other than the const static can be assigned in the class body, and the remaining variables are not assignable
int i;

Public

Default constructor
A ()
{
i=0;
printf ("a/n");
}

Copy Constructors
The main function of a copy constructor is that if the class contains pointers, you can decide
Whether the pointer needs to point to the new memory space
Otherwise, a bit copy is just a simple copy.
A (const a&)
{
I=1;
printf ("aconst/n");
}

General constructors
A (int i)
{
i=2;
printf ("aint/n");
}

= number overload
Return value if it is a, then the copy constructor is invoked if there are returns statements
a& is not
a& operator= (const a&)
{
i=3;
printf ("a=/n");
//
return *this;
/*
A A;
The copy constructor is not invoked at this time, and the compiler is optimized.
The answer should be yes. This is not optimized in the VC compiler and you still need to call
return A;
*/
}

void Speak ()
{
printf ("i =%d/n", i);
}

static void Cspeak ()
{
printf ("I ' m The class!/n");
}
};

int main (int argc, char * argv[])
{
printf ("Test 1:/n");
A A;
A.speak ();
printf ("n");

printf ("Test 2:/n");
Call a (int) first and construct a object with parameter 5
Or the 5 coercion type to a.
The default constructor is then invoked to construct a object of a
And then assign the value using the overloaded operator
A () = 5;
printf ("n");

printf ("Test 3:/n");
Not declaring an object, but declaring a function
A b ();

B (). speak ();;
printf ("n");

printf ("Test 4:/n");
A * c=new a ();
printf ("n");

printf ("Test 5:/n");
A D;
A e;
d=e;//the copy constructor is not invoked if the operator is not overloaded
D.speak ();
E.speak ();
printf ("n");

printf ("Test 6:/n");
A F;
A g=f;//Call the copy constructor at this time
printf ("n");


return 1;
}


A B ()
{
/*
A A;
A.speak ();
Do not invoke the copy constructor, the VC compiler does not optimize this, still need to call
return A;
*/

Call copy constructor
Return (* (new A ()));
}
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.