Implementation of slow evaluation!

Source: Internet
Author: User

We know that all programs are operating on the memory. at the underlying layer, the memory is used to know what to do. after knowing what to do, extract the data to be operated from the memory and put the result in the specified memory!

Let's use a simple code.

A = B + 0; // int A, B; and assign a value to a B.

If it is a built-in type, needless to say, the compiler uses the so-called emergency evaluation. That is to say, the compiler will write such a statement regardless of the context. (ADD, mov)

However, if there is such code below.

A = 1;

It seems that there are a lot of statements just now.

We are still patient with built-in access. If it is an object, the transfer of the this pointer, the assignment, and so on, the efficiency seems to be gone.

If we can avoid such a waste, it seems that we need to be more user-friendly than a compiler that says something.

This is to mention our so-called slow assessment.

That is to say, we will do the last thing until we actually use it.

For example, if you assign a value to a without having used a new value, it will be redundant. If we can obtain the corresponding operation when we can use it, it seems to be more efficient.

For example, if you don't want to define a string class as follows:

Class string

{

Char * P;

Public:

String (char *);

Char operator [] (INT );

}

In this case, both reading and writing will call the same operator [] function. even if a const version is added to the class. it is also for the const object. the compiler cannot judge the read and write operations.

In this way, we cannot write highly efficient classes, because the corresponding work must be performed in operator [] and a char is returned.

If you want to implement a slow evaluation, a substitute Class is generally defined.

Look at the code

Class string

{

Char * PTR;

Public:

Class mychar

{

PRIVATE:

String & STR; // it is very important. This is the importance of the substitute class. It can better distinguish between read and write operations. If there is no string, the substitute class will be meaningless.

Int index;

Public:

Mychar & operator = (char); // For the left Value

Operator char (); // For the right value

Mychar (string S, int II );

}

Mychar operator [] (INT );

String (char *);

}

A very simple class, but it can explain the problem

When

String S ("123 ");

Cout <s [0]; // we assume that we want to illustrate the example of the problem.

String operator [] is called as a right value to generate a mychar object. Operator char () is called because operator is not defined <to distinguish between read and write

S [0] = '1 ';

As the left value, the operator = of mychar is called, which lays the foundation for designing a slow evaluation.

We can use an emergency evaluation for the right value.

Use a slow evaluation when used as the left Value

For example

Const char a = '1 ';

S [1] =;

We can simply return bool without returning mychar & because we do nothing in operator = In the function, or we can maintain a Data Structure and retrieve this straight in operator char.

These data types are simple, and it may be silly to take a slow assessment. however, if a large software or large data structure is encountered, it will consume a lot of efficiency when they perform operations or assign values. It is a good way to adopt a slow evaluation!

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.