[8]
(Part of C++ FAQ Lite, Copyright © 1991-2001, Marshall Cline, cline@parashift.com)

İ淭룺Fnicrosoft@sunistudio.com


FAQs in section [8]:


[8.1] ʲô?

ı(һ)

þڡô(pass-by-reference)

 void swap(int& i, int& j)
 {
   int tmp = i;
   i = j;
   j = tmp;
 }
 
 int main()
 {
   int x, y;
   
// ...
   swap(x,y);
 }

˴ i j ֱmainе x y仰˵i x ָ x ָ룬Ҳ x Ŀ x i κθıͬӰ x֮Ȼ

OKΪһԱ֪áڣһͬ ǶȣܻͿǾʵֵġ͵£ x i x ĻַǣԱд i++ ʱ x Ĵ롣ϸ˵Ѱ x ĵַλûбı䡣C ԱΪ C İָ봫ݣֻǾ䷨ͬ (1) & ӵƵ˱ߴ(2)*s仰˵C ԱὫ i Ϊ (*p) p ָ x ָ루磬ԶؽDZڵָãi++ıΪ (*p)++i = 7 Զת *p = 7

Ҫ벻ҪÿΪָһָ룬ʹþûµĵַʵֵġþǶ󡣲ָָ룬ҲǶĿǶ 

TopBottomPrevious sectionNext section ]


[8.2] øֵζʲô

ıõġָʾָĶ󣩡

ס: þָʾԵıõֵʱҲıָʾֵԱдߵл˵һֵԳڸֵߣ

TopBottomPrevious sectionNext section ]


[8.3] һãζʲô

ζŸúÿԳڸֵߡ

ЩŹ֡磬û˻Ϊʽ f() = 7 塣Ȼ a һ Array ࣬˻Ϊ a[i] = 7 壬ʹ a[i] ʵһõαװ µ Array Array::operator[](int)

 class Array {
 public:
   int size() const;
   float& operator[] (int index);
   
// ...
 };
 
 int main()
 {
   Array a;
   for (int i = 0; i < a.size(); ++i)
     a[i] = 7;    
// е Array::operator[](int)
 }

TopBottomPrevious sectionNext section ]


[8.4] object.method1().method2() ʲô˼? NEW!

[Recently created thanks to Robert Cullen (on 4/01). Click here to go to the next FAQ in the "chain" of recent changes.]

Щĵã˱Ϊ

һִе object.method1()ض󣬿Ƕã磬method1() return *this һЩǹҰѷصĶΪobjectBȻobjectBΪmethod2()this

õĵطiostream⡣磬cout << x << y ִΪ cout << xһcout

ȻʹõĽ٣ȻҪյNamed Parameter Idiomʹ÷

TopBottomPrevious sectionNext section ]


[8.5] ܹʹһָһ

С

޷ָʾ롣

ָ벻ͬһúͶ󶨣޷ٱָñһûбʶ; ͼõĵַʱ㽫ĵָʾĵַסþָʾ

ij˵ int* const p  constָ const int* p ָָжôƣ벻Ҫúָ룻ȫͬ

TopBottomPrevious sectionNext section ]


[8.6] ʱʹ, ʱʹָ룿

ʹãʱʹָ롣

㲻Ҫָ(reseating)ʱһָ뱻ѡáͨζĹнӿʱáóֵĵͳǶı棬ָڶڲ

ǺIJ򷵻ֵҪһٽ硱ʱʱͨ÷/ȡһָ룬ʹ NULL ָʹ(ӦǶıDZõ NULL ָ)

ע⣺ڵߵĴ봦޷ṩĵ壬Դͳ C ԱʱϲáȻһЩ C++ ܿʶϢصһʽĶкġͬԱӦҪд룬ǻ

TopBottomPrevious sectionNext section ]


[8.7] ʲôǶľָָָָ룿ʲô NEW!

[Recently created (on 4/01). Click here to go to the next FAQ in the "chain" of recent changes.]

һָȡһķһļָ롣ǣģġ

ʵеijЩõġ磬ʱܲ׼þʾܲȷǷһ򵥵ָûָָָָõָͱʶһַԱܹԹϣhash-tableݽṹݿһЩļѯֻ֪ҪһЩΨһʶĶȡôЩͱΪ

ˣĿҪôΨһıʶ/ѯһFredָĶĻҪһFredЩ롣һܱΪ֪IJѯеļkeyʹõַ磬std::map<std::string,Fred> std::map<std::string,Fred*>еļһΪе磬Fred* array = new Fred[maxNumFreds]һ򵥵 Fred*һЩ

ѧָ߳룬ʵʹδʼָеײķա磬FredҪƶô죿FredԱȫɾʱλ֪FredҪʱģĴӴ̻ô죿ȵȡЩʱĴһӲλá磬Fred**ָFred*ָԱ֤ᱻƶFredҪƶʱֻҪָFred*ָͿˡһΪȻڱطѯFredĶ󣨻ָFredָ룩

صǵDz֪Ҫϸʱʹþ

ʹþһʱҪѾɵĶʱʱmagic cookieҲһһmagic cookieΨһʶλʵFred󡱣ѾɵĶԭʹþϸڻʾıʱӦС˵һڱвѯַΪвѯʱǿɲ´Ĵ롣

ϸڻʾıʱάΪ򵥣˵Ķдף˳װСೣoperator-> operator*ȻЧָ룬ôܿҲָ룩

TopBottomPrevious sectionNext section ]


E-Mail E-mail the author
C++ FAQ LiteTable of contentsSubject indexAbout the author©Download your own copy ]
Revised Apr 8, 2001