刘伯温四肖中特期期:

- “ϵԒ - - У· - VӅ^ɽ·A@^3307(AT)
Fڵλã ӋCӖ >> Ӗ̳ >> CZ >> 
C++̳֮̓c“Pϵ ߣӋC࡭    ̳́ԴVӋCӖ    c    •rg2010-12-1

ϴȫ www.wzfng.com     ҂ӑՓC++ĕr??/FONT>ܱ̓•“F҂׺ @}F ǣ㲻ԓʹprintɞ“•һ̓“e`
    @NfăɂҪԭǣ1̓\ڛQh“һgڄ҂̓•“òʲôЧ2•һ̓“˺Ķֿؐ҂һԓκΕr“ĺװ׻M˴惦g@ܛ]X

     ҂ȁһSr̓MoB؛Qh̓M{û̓Mĕrʲô@bһ^@Ӿ{е̓|l@朵̓oBěQh̓“҂oЫ@[a]@Ͳ“һ̓Mʲôͬ^wϵӴα^S@ӵČҪNԒǿ϶
    فһһOӋһD^҂MaterialLocationLibraryMaterialһɆTprintɆT•һ̓ṩxݔMaterialLocation

class LibraryMaterial {
private:
MaterialLocation _loc; // shared data
// ...

public:
// declares pure virtual function
inline virtual void print( ostream& = cout ) = 0;
};

// we actually want to encapsulate the handling of the
// location of the material within a base class
// LibraryMaterial print() method - we just dont want it
// invoked through the virtual interface. That is, it is
// only to be invoked within a derived class print() method

inline void
LibraryMaterial::
print( ostream &os ) { os << _loc; }

҂һBookprintݔTitle, Authorȵ@֮ǰ{ûprintLibraryMaterial::print()@ʾλãMaterialLocation

inline void
Book::
print( ostream &os )
{
// ok, this is resolved statically,
// and therefore is inline expanded ...
LibraryMaterial::print();

os << "title:" << _title
<< "author" << _author << endl;
}

AudioBookBook븽Ϣlʽȵ@Щ|printݔ@֮ǰ҂Ҫ{Book::print()@ʾǰϢ
inline void
AudioBook::
print( ostream &os )
{
// ok, this is resolved statically,
// and therefore is inline expanded ...
Book::print();
os << "narrator:" << _narrator << endl;
}

@̓{朵һֻ{õ̓]бoBQhĶԭչ_This unnamed hierarchical design pattern is significantly less effective if we never declare a virtual function to be inline.

ôڵڶԭдaÛĆ}҂һ҂£
LibraryMaterial *p =
new AudioBook( "Mason & Dixon",
"Thomas Pynchon", "Johnny Depp" );
// ...
p->print();

@printǃ“ĆȻ@Ӳòͨ^̓MC\ڛQh@׌printŗˌ㓕ˆҲ@{DQʽδa

// Pseudo C++ Code
// Possible transformation of p->print()
( *p->_vptr[ 2 ] )( p );

where 2 represents the location of print within the associated virtual function table.{printͨ^ָ_vptr[2]MеgoBěQ@{õַ@Ҳ܃“
     Ȼ̓printă“w(definition)ҲijطF fһwvirtual table{õĵַԭչ_gΛQںΕrչ_@wһg(implementaion)Ǯvirtual tableɵͬr@w@fÿһvirtual tableһw
      һɑõ[b]жvitrual tableǺ@һÆ}C++˜Ќ̓ОMҎǛ]ЌFMҎvirtual table]C++˜MҎ@@virtual table;Ҫɶقvitrual tableҲ]ҎقȻ҂ֻҪһStroustrupcfrontg̎@Щr.( Stan and Andy Koenig described the algorithm in the March 1990 C++ Report article, "Optimizing Virtual Tables in C++ Release 2.0.")
       Moreover, the C++ Standard now requires that inline functions behave as though only one definition for an inline function exists in the program even though the function may be defined in different files
µҎtҪgֻչ_һ“̓һcVõԒ̓㓵ĴaÛ}͕ʧ
[
C++ Standard: 9.3.8, Member function of local class shall be defined inline in their class defination, if they are defined at all]

============================

[a]{_N{û̓ĕrҪ^ɴg^(S. B.Lippman: Inside the C++ Object Model)
[b]
һaƷ

WУ· |Wվ؈D| ϴȫ Copyright 2006-2016 All Rights Reserved VϢԃ޹˾ 䰸/SC̖ICP11070650̖-9 B̩˱BϢg޹˾VӋCWУӖW ٷWַϴȫ www.wzfng.com ˾ַVӅ^ɽ·AWƼ@A@^3307AT