c++ primer(第四版)学习札记 8-19

王朝c/c++·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

1、感觉面试的人或者学校考试爱出这样的题:

Compare and contrast the loops you wrote in the previous two exercises. Are there advantages or disadvantages to using either form?

即while循环和for循环的好处和坏处?

sigh.....

2、while (std::cin >> value)

像这样的语句,平时很少写的吧?

不过,它利用了cin的返回值,当输入回车或类型不匹配的值时,就会让while结束

3、对于Entering an End-of-file from the Keyboard的认识,头一次知道

Operating systems use different values for end-of-file. On Windows systems we enter an end-of-file by typing a control-zsimultaneously type the "ctrl" key and a "z." On UNIX systems, including Mac OS-X machines, it is usually control-d.

4、碰到一个问题了,ex1_26

#include <iostream>

#include "Sales_item.h"

int main()

{

// declare variables to hold running sum and data for the next record

Sales_item total, trans;

// is there data to process?

if (std::cin >> total) {

// if so, read the transaction records

while (std::cin >> trans)

if (total.same_isbn(trans))

// match: update the running total

total = total + trans;

else {

// no match: print & assign to total

std::cout << total << std::endl;

total = trans;

}

// remember to print last record

std::cout << total << std::endl;

} else {

// no input!, warn the user

std::cout << "No data?!" << std::endl;

return -1; // indicate failure

}

return 0;

}

问题是:

为什么以上的对象相加用+,而不是直接用+=?

// assumes that both objects refer to the same isbn inline

Sales_item& Sales_item::operator+=(const Sales_item& rhs)

{

units_sold += rhs.units_sold;

revenue += rhs.revenue;

return *this;

}

// assumes that both objects refer to the same isbn inline

Sales_item

operator+(const Sales_item& lhs, const Sales_item& rhs)

{

Sales_item ret(lhs); // copy lhs into a local object that we'll return

ret += rhs; // add in the contents of rhs

return ret; // return ret by value

}

但是我觉得没有问题,因为成员函数中已经重载了+=操作符了

不知道大师问这个问题的含义,我试着编了一个程序,没有碰到问题。

5、wchar_t

用于表示双字节的字符,中文,呵呵

比如:L"中文",用L做前缀,表示双字节

6、Unlike the other integral types, there are three distinct types for char: plain char, signed char, and unsigned char. Although there are three distinct types, there are only two ways a char can be represented. The char type is respresented using either the signed char or unsigned char version. Which representation is used for char varies by compiler.

不懂得usigned char干什么用的

7、In C++ it is perfectly legal to assign a negative number to an object with unsigned type. The result is the negative value modulo the size of the type. So, if we assign 1 to an 8-bit unsigned char, the resulting value will be 255, which is 1 modulo 256.

c++确实很自由,可以把负数负给unsigned变量却不出错,在灵活的同时,要提醒自己不要掉进陷阱

8、作者建议我们使用double来代替float,因为这样精度提高了,效率却影响不大,在某些机器上,双精度甚至比单精度效率还高。而整型则推荐用int,在32bit的系统中相当于long,基本满足需要

9、和java不同,c++并不使用+操作符来连接长字符串

// concatenated long string literal

std::cout << "a multi-line "

"string literal "

"using concatenation"

<< std::endl;

10、c++自由的体现

// ok: A \ before a newline ignores the line break

std::cou t << "Hi" << st d::endl;

这样的换行经常出现在宏的定义中

11、std::string提供了一种简单的直接初始化的方法:

std::string all_nines(10, '9'); // all_nines= "9999999999"

12、extern关键字的用法:

extern double pi = 3.1416; // definition

extern double pi; // ok: declaration not definition

extern double pi = 3.1416; // error: redefinition of pi

在使用extern可以声明一个变量,但不是定义,在程序中可以多处声明,却不能多处定义变量。

13、不同于变量,在定义const的时候,为了让不处于同一文件的程序可以访问得到,要在定义的时候之前加上extern:

// file_1.cc

// defines and initializes a const that is accessible to other files

extern const int bufSize = fcn();

// file_2.cc

extern const int bufSize; // uses bufSize from file_1

// uses bufSize defined in file_1

for (int index = 0; index != bufSize; ++index)

// ...

而变量按照常规方式定义就可以,在其他文件中使用的时候,用extern声明一下就ok,因为变量默认有extern属性

14、const Reference is a Reference to const

这点很重要,一开始我还以为是常量引用,而实际上是对常量的引用,其实引用从定义的一刹那就不能改变指向了,所以引用不可变,和指针不同,对常量的引用是改变不了常量的值的。

——end—— next:2.8. Class Types

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航