14th Section 14.9.2 Exercises & 14.9.3 Exercises

Source: Internet
Author: User

Exercise 14.50

What types of conversion sequences might be used to initialize EX1 and ex2? Explains whether initialization is correct and explains why.

struct longdouble{
  longdouble (double = 0.0);
  operator double ();
  operator float ();

Longdouble ldobj;
int ex1 = ldobj;
float ex2 = ldobj;

Answer:

(1) There is no problem with type object creation.

(2) in the int ex1 = ldobj; There is a problem with this sentence.

There will be two alternative conversion sequences:

Double-> int

float-> int

Thus resulting in two semantics, the compiler will error when compiling.

(3) Float ex2 = lobobj; there's no problem with that.

Because there's only one conversion sequence here:

Double-> float;


Exercise 14.51

What types of conversion sequences might be used in the process of calling Calc again? Shows how the best possible function is chosen.

void calc (int);
void Calc (longdouble);
Double dval;
Calc (dval); Which CLAC?
Answer:

The conversion sequence used here is a double-> int

Because Longdouble is a custom type, the compiler chooses the built-in type for precedence conversions.

But when we remove the calc (int) function, calc (longdouble) also executes, and this time there will be an implicit conversion, longdouble-> double, and the function will accept the double type argument.


Exercise 14.52

The operator+ is selected in the addition expression below. Lists the candidate functions, the feasible functions, and the type conversions performed by the arguments for each feasible function:

struct longdouble{
//Member operator+ for presentation; In general, + is a non member
longdouble operator+ (const SMALLINT &);
Other members are consistent with section 14.9.2 (page No. 521)
};

Longdouble operator+ (Longdouble&, double);
SmallInt si;
Longdouble ld;
ld = si + ld;
LD = ld + si;
Answer:

ld = si + ld; With the following two candidate functions, you can see from the following two candidate functions what type of conversion

operator+ (int, double) <built-in>

operator+ (int, float) <built-in>

SmallInt-> int, longdouble->double or longdouble-> float


ld = ld+ si; With the following two candidate functions, you can see from the following two candidate functions what type of conversion

operator+ (double, int) <built-in>

operator+ (float, int) <built-in>

The type conversion sequence is the same as the above.


Exercise 14.53

Erection we have defined the smallint as shown on page No. 522 to determine whether the following additive expression is legitimate. If valid, the addition operator is used. If not, how to modify the code to make it legal.

SmallInt S1;

Double d = s1 + 3.14;

Answer:

Illegal, or two semantic, the following two exercises as a candidate operation

operator+ (int, double) <built-in>

SmallInt operator+ (const smallint&, const smallint&)


Double d = static_cast<double> (S1) + 3.14;
Forcing type conversions on S1 allows this code to be executed legally.

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.