本文介绍了Cline / Lomow Book(Original)FAQ#158的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 FAQ 158是关于OO的正确使用。我很难理解 如何选择正确的函数。来自base classPrinter2的三个派生类。从三个过度骑行中选择的逻辑在哪里?那个小装饰p在最后一行代码中, 似乎是关键。这个新秀就是没有得到它。感谢任何 帮助。 以下是FAQ 158程序的副本: === ====================================== class Printer2 { public: virtual~Printer2(){} virtual void italics(const char * s)= 0; }; 类EpsonPrinter2:public Printer2 { public: virtual void intalics(const char * s) {cout<< esc<< " 1 +" << s<< esc<< "异英寸; } }; 类ProprinterPrinter2:public Printer2 { public: virtual void intalics (const char * s) {cout<< esc<< " [I" << s<< esc<< " [N英寸; } }; class StarPrinter2:public Printer2 { public: virtual void intalics (const char * s) {cout<< esc<<英寸×" << s<< esc<< " Y英寸; } }; void printUsingItalics(Printer2& p,const char * s) { p.italics(s); } FAQ 158 is about correct usage of OO. I am struggling to understandhow the correct function is selected. Three derived classes from baseclass "Printer2". Where is the logic that selects from the threeover-rides? That small decoration "p" in the next to last line of codeseems to be the key. This rookie just doesn''t ''get it''. Thanks for anyhelp. Below is a copy of the FAQ 158 program: ========================================= class Printer2 {public :virtual ~Printer2( ) { }virtual void italics(const char* s) = 0;}; class EpsonPrinter2 : public Printer2 {public :virtual void intalics(const char* s){ cout << esc << "i+" << s << esc << "i-"; }}; class ProprinterPrinter2 : public Printer2 {public :virtual void intalics(const char* s){ cout << esc << "[i" << s << esc << "[n"; }}; class StarPrinter2 : public Printer2 {public :virtual void intalics(const char* s){ cout << esc << "x" << s << esc << "y"; }}; voidprintUsingItalics ( Printer2& p, const char* s){p.italics(s);}推荐答案 由于斜体是一个虚函数,因此它是动态绑定的。在其他 字样中,斜体的确切版本是从参考p所持有的实际类别 中选择的。 Since italics is a virtual function, it is bound dynamically. In otherwords, the exact version of italics is selected from the actual classheld by the reference p. 您好, http://www.parashift.com/c++-faq-lit...functions.html 有一个很好的解释[见20.4] 关于marcas Hi, http://www.parashift.com/c++-faq-lit...functions.html has a good explanation [see 20.4] regards marcas 由于斜体是一个虚函数,它是动态绑定的。换句话说,斜体的确切版本是从参考p所持有的实际类中选出的。Since italics is a virtual function, it is bound dynamically. In otherwords, the exact version of italics is selected from the actual classheld by the reference p. 感谢Neelesh的回复。我绝对在向OO过渡到过程中挣扎。 C ++常见问题解答中的一些示例远比我现在的技能水平高出多少b $ b。我猜的例子不是完整的代码块吗?特别是缺少main()?你的回复让我重新阅读了关于参考文献和参考文献的第23章。 (130 页面深入到本书中。我已经阅读了封面封面,并且 必须反复出现。) 所以要使代码完整...需要在 后面添加一些东西? main() { StarPrinter2 p; //创建派生对象 //类型StarPrinter2 char textString [] ="我有一个梦想要学习OO。; printUsingItalics(p,textString); } Thanks for the reply Neelesh. I am definately struggling in thetransition to OO. Some of the examples within "C++ FAQs" are far moreadvanced than my current skill level. The examples I guess are notcomplete blocks of code? In particular the main( ) is missing? Yourresponse made me reread chapter #23 on references and referents. (130pages deeper into the book. I have read it cover-to-cover and willhave to repeatedly it seems.) So to make the code complete ... one needs to add something along thefollowing lines? main ( ){StarPrinter2 p; // Creating A Derived Object// Of Type StarPrinter2char textString[] = "I have a dream to learn OO.";printUsingItalics( p , textString );} 这篇关于Cline / Lomow Book(Original)FAQ#158的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 10:00