《C++ FAQ》读书笔记

FAQ 21.02 What is a virtualmember function?

答:

the same ...as  和...相同

 

signature     n.署名、识别标识.(在这儿是指被编译器修饰过后的对应函数名)

 

invoke     v.调用

 

override   v.覆盖

 

A member function should be madevirtual(写成虚的) when there will be derived classes that will need to providetheir own implementation(实现) for the member function. Thisdoesn't require as much clairvoyance(n.洞察力) as it seems to imply. Normally thevirtual functions represent specifically architected places where extensibilityis supposed to take place(发生).

难翻译!!!!!!

 

straightforward     adj.简单的

 

FAQ 21.03 How much does it costto call a virtual function compared to calling a normal function?

答:

highly  adv.很大程度上

in theory 从理论上说

in practice 实际上

several    adj.较少的

 the work that gets done    它所做的工作

 

FAQ 21.04 How does C++ performstatic typing while supporting dynamic binding?

答:

Static typing  静态定型

plumbing   n.管道(可以理解成方法)

Given 假定

there are two distinct(不同的) typesin question(正在被讨论):

有两个不同的类型需要讨论:

 

the static type of the referenceand the dynamic type of the referent (that is, the object being referred to).

引用(或指针)本身的类型和引用(或指针)所引用对象的类型

 

reference   n.引用本身

referent    n.被引用的对象 

 

as well    同样、也

 

 

FAQ 21.05 Can destructors bevirtual?

答:

Virtual destructors are extremely(非常地)valuable(有价值的) when some derived classes(子类)have specified cleanup code. A practical(实用的), easy-to-remember guideline(原则):if a class has any virtual functions, it should have a virtual destructor. Therationale(基本原理) for this is that if a class has no virtual functions, chances are the class designer wasn't planning on the class being usedas a base class, so a virtual destructor is unnecessary.

这个原则的基本原理是:如果一个类没有虚函数,那么,这个类的设计者非常可能没有计划把这个类用作一个基类,所以一个虚析构是不必的。

 

Furthermore(而且), on mostcompilers there is no additional per-object space cost after the first virtualfunction, so there is very little reason not to (没有理由不)make the destructorvirtual if the class already has at least one virtual function.

 

nontrivial   adj.有用的

 

FAQ 21.06 What is the purpose ofa virtual destructor?

答:

while compilingunawareOfDerived(Base*), the compiler doesn't even know that Derived exists,much less that the pointer base may actually be pointing at a Derived.

当编译unawareOfDerived(Base*)的时候,编译器甚至连Derived存不存在都不知道,更不用说指针base实际上可能指向一个Derived.

 

虚析构函数是如何实现的?

答:经过分析实验得出如下结论:

当析构函数不是虚函数时,对象销毁时调用的那个析构函数是固定了的.

 

当析构函数是虚函数时,它的函数地址会根据虚函数的声明顺序依次放入虚表.

 

当有一个类继承了一个拥有虚析构函数的类时,子类会自动继承基类的虚表,当然基类的析构函数也会被继承.但值得注意的是,子类的析构函数会自动成为虚函数,并且这个虚函数在虚表中的位置是和基类的析构函数的地址是相同的,也就是说把基类的析构函数覆盖掉了.

 

当编译器编译delete base;时,只要根据实际对象的地址找到虚表指针,再根据析构函数在虚表中的偏移地址就可以找到实际该调用的函数了.

 

其实,虚析构函数和一般的虚函数的原理是一样的,唯一的不同点是虽然基类的析构函数名和子类的析构函数名虽然不同,也可以产生覆盖效果!!而一般的虚函数必须函数名完全相同才能产生覆盖效果!

 

FAQ 21.07 What is a virtualconstructor?(这是新东西!!!!)

答:

and until there is a livingobject against(靠着) which to invoke a member function, the member functioncannot possibly work correctly.

因为靠着a living object才能调用调用成员函数,所以在有一个aliving object之前,成员函数不可能正常工作!

 

例:

靠着,依着

to lean against the wall

靠着墙

 

Instead of(代替) thinking ofconstructors as normal member functions on the object, imagine that they arestatic member functions (see FAQ 16.05) that create objects.

 

idiom n.语言习惯用法

 

FAQ 21.08 What syntax should beused when a constructor or destructor calls a virtual function in its object?

答:

也就是说,虽然我们在构造函数中调用本类的虚函数不会出错,但是为了减少误解,最好加上作用域限制符::

原文如下:

Since developers are oftensomewhat surprised(惊讶) by this language feature, we recommend that such callsshould be explicitly qualified(修饰) with the scope operator, ::.

 

analogous  adj.类似的 

 

这句话的语法不好理解.

In our experience, this guidelinereduces the probability that misunderstandings will introduce subtle defects,

 

qualify    vt.修饰

 

 

FAQ 21.09 Should the scopeoperator :: be used when invoking virtual member functions?

答:

 

 

The purpose of the scope operatoris to bypass(vt.绕过,避开) the dynamicbinding mechanism.

 

dubious   adj. 含糊的

 

FAQ 21.10 What is a pure virtualmember function?

答:

 

abstract base class (ABC)

 

Imagine vt.可以想象成

 

FAQ 21.11 Can a pure virtualfunction be defined in the same class that declares it?

答:

Yes, but new C++ programmersdon't usually understand what it means, so this practice should be avoided ifthe organization rotates developers.

这句话怎么理解?

 

FAQ 21.12 How should a virtualdestructor be defined when it has no code?

 答:

It should normally be defined asan inline virtual function.

 

FAQ 21.13 Can an ABC have a purevirtual destructor?

答:

Yes, provided(倘若) the ABC(abstract base class) gives an explicit definition elsewhere.

也就是说,虽然析构函数是纯虚的,但是必须定义.

 

Leave out  vt.遗漏

 

marginal adj.边际的

 

Calls to inline virtual functionscan be inlined if the compiler is able to statically bind to the class. 尽管如此,只要编译器能够静态绑定到一个析构调用,编译器也可能内联它.

 

FAQ 21.14 How can the compiler bekept from generating duplicate outlined copies of inline virtual functions?

答:

If a class has one or morevirtual functions , then the class should have at least one non-inline virtualfunction.

 

keep ...from   抑制...做...

 

magical(神秘的,因为我们看不见,所以神秘) stuff(成员)

 

out-lined copies of inline virtual functions  

out-lined怎么翻译?

 

sensitive adj.易受影响的

 

But even in these compilers, itdoesn't cost much to ensure that at least one of the class's virtual functionsis non-inline.

怎么理解:

1.不会花太多去干...?没干

2.去干...不会花太多?干了

 

FAQ 21.15 Should a class withvirtual functions have at least one non-inline virtual function?

答:It is a good idea.

 

bet  n.意见

 

 

析构函数和构造函数刚好相反,当子类的析构函数完成后,对象的虚表指针值马上改变成父类的虚表地址.(魏芸已验证)例子如下:

子类和父类的析构函数中分别调用本类的一个虚函数.

 

 

Chapter 19. Friend Classes andFriend Functions

 

FAQ 19.01 What is a friend?

答:

A friend is an entity(实体) towhich a class grants access authority(权力).

友元是一个实体,而类给予了这个实体访问类成员的权力.

 

Friends can be functions, other classes, or individualmember functions of other classes.

 

For instance 例如

 

conclude  vt.关闭

 

abstraction   n.抽象

 

arbitrary  adj.任意的

 

impose vt.强加

 

FAQ 19.02 What's a good mental(存在于脑中的)model for friend classes?

答: 

 

confidant n.知己,心腹朋友

 

realm  n.领域

 

The alternative(二者选其一的) togranting special access privileges between the classes would be for the classes todeclare public: member functions that allow anyone to manipulate the class'sprivate: members.

be for     vi.被赞成

 

In the traditional softwarerealm, friendship is called tight cohesion, and is, within limits, considered good.

强聚合

 

FAQ 19.03 What are someadvantages of using friend classes?

答:

 

coupled  adj.联系的

 

arise vi.出现

 

FAQ 19.04 Do friends violate(违反)the encapsulation barrier?

答:

barrier n.屏障

 

naive adj.幼稚的

 

enlightened   adj.进步的

 

interrelationship n.相互关系

 

Friends don't violate theencapsulation barrier; they are part of the encapsulation barrier.

 

FAQ 19.05 What is a friendfunction?

答:

 

FAQ 19.06 When should a functionbe implemented as a friend function rather than a member function?

答:

Whenever it improves readabilityof user code.

 

intuitive adj.直观的

 

point n.用途

 

FAQ 19.07 What are someguidelines to make sure friend functions are used properly?

答:

tricky  adj.微妙的

innards  n.内部结构

in a position to adv.能够

 

FAQ 19.08 What does it mean thatfriendship isn't transitive(传递的)?

答:

(the public: section is wherenormal users look to find out how to use a class).

所以说,如果不是外部接口的话,千万不要声明为public

 

FAQ 19.09 What does it mean thatfriendship isn't inherited?

答:

 

FAQ 19.10 What does it mean thatfriends aren't virtual?

答:

one-line   adj.同样的

 

FAQ 19.11 What qualities(性质)suggest a friend function rather than(而不是) a member function?

答:

The operator* needs to be afriend because C++ never automatically promotes the this object in a memberfunction invocation.

 

The three P's of friendship:position, promotion, or perception(理解).

 

suggest vt.要求成为

 

FAQ 19.12 Should friend functionsbe declared in the private:, protected:, or public: section of a class?

答:

 

FAQ 19.13 What is a privateclass?

答:

 

FAQ 19.14 How are objects of aclass printed?

答:

#include <iostream>

using namespace std;

 

class Fred {

public:

    friend ostream& operator<< (ostream& ostr, const Fred& x) throw();

private:

 int i_;

};

 

ostream& operator<< (ostream& ostr, const Fred& x) throw()

{

 ostr << x.i_;

 return ostr;

 

}

 

int main()

{

    Fred f;

    cout<<f;

 

    return 1;

}

以上代码在VC中编译不能通过!解决办法:

#include <iostream>

using namespace std;

改为:

#include <iostream.h>

就可以了.看来VC对C++标准的支持不太好!!!!

 

 

FAQ 19.15 How do objects of aclass receive stream input?

答:

 

Chapter 14. Const Correctness(正确性)

 

FAQ 14.01 How should pointerdeclarations be read?

答:

 inherently  adv.天生地

 immutable   adj.不可变的

 

FAQ 14.02 How can C++ programmersavoid making unexpected(意外的) changes to objects?

答:

如果是引用,只要没有const,那么都意味着改变.只要你不打算改变别人传进来的参数,那么最好声明为const.

const的对象不可以传递给非const对象.

非const对象可以传递给const对象.

 

==>总结为,限制少的可以传递给限制多的.反过来不可以.

 

FAQ 14.03 Does const imply(意味着)runtime overhead?

答:

 

FAQ 14.04 Does const allow thecompiler to generate more efficient code?

答:

Occasionally(偶尔), but that's notthe purpose of const.

 

However, const correctnessguarantees(为...提供保证) even tighter(严厉的) semantic(语义的) correctness by making surethat data that is not intended(打算) to be changed cannot be changed.

 

With const correctness, it iseasier to reason(推究) about the correctness of thesoftware.

 

It is almost as if conststring and string are of different, but(除了) related, classes.

除了相关,const string和string几乎好像是不同的类.

 

FAQ 14.05 Is const correctnesstedious?

答:

It is no more(只不过) tedious(冗长的) than declaring the type of a variable.

 

succinct adj.简洁的

 

FAQ 14.06 Why should constcorrectness be done sooner(立刻) rather than later?

答:

ripple  n.小波浪

ripple effect 连锁效应

 

If a function was not originallyrestricted with respect to(关于) changing aby-reference parameter, adding the restriction (that is, changing a parameterfrom string& to const string&) can cause a ripple through the system.

如果一个函数在最初没有限制改变一个引用传递参数,以后添加这个限制可能在系统中引起一个连锁反应.

 

moral  n.教训,寓意

 

FAQ 14.07 What's the differencebetween an inspector and a mutator?

答:

没有inspector和mutator,看来是C++中专用的.

 

observable  adj.显而易见的

 

FAQ 14.08 When should a memberfunction be declared as const?

答:

flaw n.缺陷

 

flesh out  使...有血有肉

 

Hopefully these two approachesend up agreeing with each other.

但愿这两种方法最终是一致的.

 

FAQ 14.09 Does const apply to(应用于)the object's bitwise state or its abstract state?

答:

modifier n.修饰成分

 

FAQ 14.10 When should const notbe used in declaring formal(形式的) parameters?

答:

Do not use const for formalparameter types that are passed by value.

 

FAQ 14.11 When should const notbe used in declaring a function return type?

答:

A function that returns itsresult by value should generally avoid const in the return type.

 

idiomatic  adj.合乎语言习惯的

 

FAQ 14.12 How can a"nonobservable" data member be updated within a const memberfunction?

答:

Preferably adv.更适宜

 

FAQ 14.13 Can an object legallybe changed even though there is a const reference (pointer) to it?

答:

Yes, due to(因为) aliasing(混淆).

 

 

FAQ 14.14 Does const_cast meanlost optimization opportunities?

答:

 

 

Chapter 22. Initialization Lists

 

FAQ 22.01 What are constructorinitialization lists?

答:

exercise vt.实行

 

FAQ 22.02 What will happen ifconstructor initialization lists are not used?

答:

 

FAQ 22.03 What's the guidelinefor using initialization lists in constructor definitions?

答:

 

FAQ 22.04 Is it normal for constructors to have nothing inside theirbody?

答:

 

FAQ 22.05 How is a const data member initialized?

答:

 

FAQ 22.06 How is a reference data member initialized?

答:

 

FAQ 22.07 Are initializers executed in the same order in which theyappear in the initialization list?

答:

irrelevant adj.不相关的

 

FAQ 22.08 How should initializers be ordered in a constructor'sinitialization list?

答:

mimic vt.模仿

 

This guideline discourages( 阻止) a particularly subtle(微妙的) class oforder dependency(相依性) errors by giving an obvious, visual clue(线索).

 

hideous adj.可怕的

 

If the guideline espoused(vt.支持) by this FAQ was employed(使用),

 

FAQ 22.09 Is it moral for one member object to be initialized usinganother member object in the constructor's initialization list?

答:

 

FAQ 22.10 What if one member object has to be initialized using anothermember object?

答:

 

FAQ 22.11 Are there exceptions to the rule "Initialize all memberobjects in an initialization list"?

答:

Yes, to facilitate(使...容易) argument screening(or checking,检查).

 

FAQ 22.12 How can an array of objects be initialized with specificinitializers?

答:

 

 

Chapter 13. Inline Functions

FAQ 13.01 What is the purpose of inline functions?

答:

 In some cases, inline functionsmake a compute-bound application run faster.

在某些情况下

 

in a sense

就某种意义来说

 

 As usual, the devil(罪恶) is in the details. Read thefine print; it does make a difference.

 

inline和inlined有很大的不同.inline只是请求编译器将函数内联,实际内联没不一定.而inlined表示这个函数真的被内联了.

 

FAQ 13.02 What is the connection between the keyword "inline"and "inlined" functions?

答:

被inline修饰的函数不一定被内联,内联了的函数不一定被inline修饰.根本没有方法让一个函数一定被内联.

 

内联的规则是特定于编译器的.

 

FAQ 13.03 Are there any special rules about inlining?

答:

任何使用内联函数的源文件都必须包含内联函数的函数体,所以最好的解决办法是让内联函数的的定义放在头文件中.

glitch n.小故障, 失灵

 

FAQ 13.04 What is the one-definition rule (ODR)?

 

FAQ 13.05 What are some performance considerations with inlinefunctions?

 

FAQ 13.06 Do inlined functions improve performance?

答:

pursue vt.从事

 

compute-bound

CPU-bound 受限于...

thrashing n.捶击

 

FAQ 13.07 Do inlined functions increase the size of the executable code?

答:

 

FAQ 13.08 Why shouldn't theinlining decision be made when the code is first written?

答:

 frustration  n.使人失望的事

crummy  adj.无用的

 

FAQ 13.09 What happens when aprogrammer uses an inlined function obtained from a third party?

答:

innocuous  adj.无害的

 

FAQ 13.10 Is there an easy way toswap between inline and non-inline code?

答:

Most projects should turn offinlining during development. That is, they use a compiler option that causesthe compiler to not inline any inline functions.

 

 

Chapter 15. Namespaces

FAQ 15.01 What is the purpose ofthis chapter?

答:

accentuate n.强调

 

FAQ 15.02 What is a namespace?

答:

 

FAQ 15.03 How can code outside anamespace use names declared within that namespace?

答:

 

FAQ 15.04 What happens if twonamespaces contain the same name?

答:

 

FAQ 15.05 What are some of therules for using namespaces?

答:

 

FAQ 15.06 What is name lookup?

答:

 

FAQ 15.07 What are the tradeoffs(折衷)between the various techniques for using names from a namespace, particularlythe standard namespace?

答:

solely     adv.单独的

Clutter    n.混乱

breakage   n.破坏

 

FAQ 15.08 Can namespaces breakcode?

答:

insidious adj.阴险的

exotic        adj.奇异的

 

FAQ 15.09 Do namespaces have anyother applications? 

答:

clean up  清除

knotty adj.棘手的

 

Second, using declarations arenow the preferred alternative to access declarations that were used to workaround some knotty problems with private inheritance.

这一点不太理解!

 

FAQ 15.10 How do namespaces solvethe problem of long identifiers?

答:

namespace CWLN =CompanyWithLongName;

使用名字空间的别名

 

Chapter 11. References

 

FAQ 11.01 What is a reference?

答:

alternate name  n.又名

 

FAQ 11.02 What does"referent" mean?

答:

例:

j is the reference and i is the referent.

int main()

{

 int  i;

 int& j = i;

}

 

FAQ 11.03 When can a reference beattached to its referent?

答:

如果类有一个引用成员变量,可以在构造函数初始化列表中初始化。就跟初始化const成员变量一样。

所以,完全可以把引用看作是一个const指针。  

 

FAQ 11.04 What happens when avalue is assigned to a reference?

答:

 

const int& j=8;

汇编代码:

00401028   mov        dword ptr [ebp-8],8

0040102F   lea        eax,[ebp-8]

00401032   mov        dword ptr [ebp-4],eax

 

FAQ 11.05 What is a localreference?

答:

 

 

FAQ 11.06 What does it mean toreturn a reference?

答:

subscript n.下标

 

FAQ 11.07 What is the result oftaking the address of a reference?

答:

The address of a reference is theaddress of the referent.

 

如何取得一个引用的地址?

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值