Sequence point, constructor and so on in C + + C + +

Source: Internet
Author: User

This article will briefly discuss the following questions what is the sequence point C + + constructor Why there is no return value How do i show call constructors and destructor copy constructors Why must be a reference pass reason why static and const cannot be used in class member functions 1. What is sequence point

In today's written test, we often encounter the following questions, when the following statements are executed, a value is what?

int a = 1; No side effect
A = a + +;  Side effect

The answer is simply provided, 1, 2, undefined ...
In fact, the above example, in C + + has a corresponding noun sequence point.

A sequence point are a point at the program's exexution sequence where all previous side-effects shall have take place and where all subsequent side-effects shall is not have take place.

In C + +, there are two types of expression computations, one without side effects, but with side effects (side effect). As shown in the preceding code.
And a sequence point as defined above, all side effects of the expression before the dot occurs before the execution of the program arrives at a pity Dorado, and all side effects of the expression after that point have not occurred until the program execution reaches that point. Rule 1 When an expression is evaluated, the value stored by an object can only be modified at most once in the previous and next order points. The results of the following figure are undefined
Rule 2 Changes the value of an object when the expression is evaluated, and the only purpose for which the value before the change is read is to determine that the new value is stored.

In C, the specified sequence point is very small, as this is beneficial to the compiler's optimization maximization. 2. Why does the constructor for C + + have no return value

On a simple example to help understand and remember.

Class Base {
//...
};

void foo (int a) {
    //do something ... 
}

void foo (const base& Base) {
    //do something ...
}

int main ()
{
    //If the constructor returns a value, the following result will call which function.
    foo (Base ()); 
    return 0;
}

Obviously, the constructor does not set the return value because it is determined by the special properties of the constructor. If there is a return value, then there will be a lot of problems. 3. How to display call constructors and destructors

In fact, when we take the new operator, the following three things usually happen: Call:: operator new Assign the constructor of the required memory call object returns a pointer to the newly allocated and constructed object

We use the following code to simulate this process. The results show that we are always the call constructor and destructor that is displayed.

Class Base
{public
:
    base () {cout << "constructors" << Endl;}
    ~base () {cout << "destructors" << Endl;}
};

int main ()
{
    Base *PB = (Base *) malloc (sizeof (base));
    New (Pb) Base ();
    Pb->~base ();
    Delete PB;

    return 0;
}
4. Why the copy constructor must be a reference pass

Before delving into this, there are several scenarios for copying initialization: When using = To define a variable, such as Foo NEWOBJ = Oldobj; Passing an object as an argument to an argument of an unreferenced type; Returning an object from a function of a return type that is not a reference type; Initializes an element in an array or a member of an aggregate class with curly braces, and some class types use copy initialization for the objects they assign. For example, when we initialize a standard library container or call an INSERT or push member, the container copies it to initialize it. Elements created with Emplace members are initialized directly.

Class Base
{public
:
    base () {cout << "constructors" << Endl;}
    Base (const base BS) {
        a = Bs.num;
    }
    ~base () {cout << "destructors" << Endl;}

Private:
    int num;
};

As mentioned above, parameters with unreferenced types are copied and initialized during function calls. Similarly, when a return type with a unreferenced type is returned, the return value is used to initialize the caller's results.
The copy constructor is used to initialize the unreferenced class type parameter, and if the parameter is not a reference type, it will go into the dead loop –> because its arguments must be copied in order to invoke the copy constructor, but in order to copy the arguments, we need to call the copy constructor, so that the infinite loop . 5. The reason why static and const cannot be used in class member functions

One of the design guidelines for C + +: The nonstatic member function must be at least as efficient as the nonmember function.
For example:

Float Magnitude3d (const Point3D *_this) {//...}
float Point3d::magnitude3d () const {//...}

For the latter's nonstatic member function, it will eventually expand to:

Point3D point3d::magnitude (const Point3D *const this) {
    ///...
}

Visible from above, theconst modifier is the pointer this, and this belongs to the category of object. For static , a static member function or member variable in a class is not a specific object, and its category is the entire class. Therefore, the categories of modifications are different, so that static and const cannot be used with the same member function or member variable in the class .

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.