作者adrianshum (Alien)
看板C_and_CPP
标题Re: [问题] 看Stroustrup 的C++後有不懂的问题想请教
时间Tue Mar 17 12:50:46 2009
※ 引述《allstarschh (allstars)》之铭言:
: 我看的是Stroustrup的the C++ Programming lang
: 然後有几个问题想请教
: 1.在Chap3 3.11 Advice p.66 里 第七条
: Using string rather than char*
: 请问即使我对char * 用了简单的strcat strcpy这些library
: 会比string 还要来的慢吗???
: 或者是可以说明在处理字串时(就ascii就好了)
: 什麽时候用string,when to use char*呢?
不是速度问题, 而是安全简单的原因.
用 char* 你要记得是自己 allocate 还是单
纯指向, 要知道 allocate 了多少位置, 不够
位置又要自己 re-allocate etc, 这些都是 error-prone
的东西.
什麽时候用什麽.... 我会答, 没有什麽特别
原因, 字串就用 std::string 吧. char* 就留在真
的需要的时候才用 (到那个时候你就会知道 string
不合用了)
: 2.在10.2.8 Structurs and Classes的最後 p.235
: 他讲了一句
: allowing many access sepcifiers in a class is useful for
: machine generated code.
: 就是他上面有讲个例子 public,private可以一直加的
: class Data4 {
: public :
: ...
: private :
: ...
: public:
: ...
: }
: 这里的machine generated code是指什麽?
: 是某些tool产生出的c++ code还是compile完产生的code
: (IR, assembly, or machine code)
: 为什麽会useful??
意思是, 比如我从 CORBA 的 IDL 或 Webservice 的 WSDL
之类, 经由一个 tool 建立一个对应的 C++ source file,
当中可能每个 method 都要建立一些对应的 private helper method.
那麽我的 tools 可以这样做:
while(idl.hasMoreMethod()) {
methodName = idl.getMethod();
outputSource.print("public:");
outputSource.print("void " + methodName + "();";
outputSource.print("private:");
outputSource.print("void " + methodName + "Helper();";
}
这样我 generate 出来的 source 就会有很多部份的 public:/private:
C++ 容许这样的写法, 就让我的 generation 过程变简单了, 不然我就得
把所有 public method 先存着, 然後到读完 input file 才一次写入, 就
比较麻烦了
: 3. 在10.4.6.2 Member Constants p249
: 他说可以initialize a static integral constant member
: 为什麽只有int 可以 float那些为什麽不行呢???
书不在手边 :P 不知前文後理了
: 4.在11.2.3 p265
: 第一段要结束时他说
: it is not possible to define an operator function that
: operates exclusively on pointers
: 这是指什麽意思呢
: 感谢
即是不可能做
int operator +(Foo* rhs, Bar* lhs);
Foo* foo;
Bar* bar;
int result = foo + bar;
这样的事.
道理上就等如不允许你定义一个operator
只有 int 一样. 如果写了
int operator +(int rhs, int rhs);
可想是多大的灾难
alien
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 202.155.236.82