tailieunhanh - Thinking in C plus plus (P16)
Tham khảo tài liệu 'thinking in c plus plus (p16)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | class Multiple inheritance will solve the problem. So you do the following a similar example was given at the end of Chapter 15 Now OShape has Shape s characteristics and behaviors but because it is also derived from Object it can be placed in Container The extra inheritance into OCircle OSquare etc. is necessary so that those classes can be upcast into OShape and thus retain the correct behavior. You can see that things are rapidly getting messy. Compiler vendors invented and included their own object-based container-class hierarchies most of which have since been replaced by template versions. You can argue that multiple inheritance is needed for solving general programming problems but you ll see in Volume 2 of this book that its complexity is best avoided except in special cases. The template solution Although an object-based hierarchy with multiple inheritance is conceptually straightforward it turns out to be painful to use. In his original book3 Stroustrup demonstrated what he considered a preferable alternative to the object-based hierarchy. Container classes were created as large preprocessor macros with arguments that could be substituted with your desired type. When you 3 The C Programming Language by Bjarne Stroustrup 1st edition Addison-Wesley 1986 . 730 Thinking in C www. BruceEckel .com wanted to create a container to hold a particular type you made a couple of macro calls. Unfortunately this approach was confused by all the existing Smalltalk literature and programming experience and it was a bit unwieldy. Basically nobody got it. In the meantime Stroustrup and the C team at Bell Labs had modified his original macro approach simplifying it and moving it from the domain of the preprocessor into the compiler. This new code-substitution device is called a template4 and it represents a completely different way to reuse code. Instead of reusing object code as with inheritance and composition a template reuses source code. The container no longer holds a
đang nạp các trang xem trước