C++ has a powerful mechanism for operator overloading. Beside operator such as
+, -, =, ==, type name can also be overloaded as operator. Here is an example.
It allows Device to be implicitly converted into DeviceHandle.
class DeviceHandle {
};
class Device {
public:
Device(DeviceHandle dh) : _dh(dh) {
}
operator DeviceHandle() const {
return _dh;
}
private:
DeviceHandle _dh;
};
void updateDeviceHandle(DeviceHandle dh) {
}
int main(int argc, const char *argv[]) {
DeviceHandle dh1;
Device d1(dh1);
updateDeviceHandle(d1);
return 0;
}
分享到:
相关推荐
oriented paradigm and an overview of C++, it gradually moves on to analyse in detail important concepts such as classes, objects, functions, constructors and destructors, operator overloading, ...
: Construct The Comma Operator Overloading the ( ) Operator Pointers to Members The asm Statement The mutable Qualifier Run Time Type Identification Trigraphs Answers to Chapter Questions 30....
Operator Overloading, Type Conversion, New Style Casts, and RTTI Chapter 9. Data Structures Chapter 10. Templates Chapter 11. Exception Handling Appendix A: Case Study—A Word Query System Appendix B...
Other C++ Features Reference Arguments Function Overloading Default Arguments Variable-Length Arrays and alloca() Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and ...
Operator Overloading, Templates Exception Handling, Files Bit and Character Manipulation Boost Libraries and the Future of C++ GNU™ and Visual C++® Debuggers And more… VISIT WWW.DEITEL.COM For ...
Written by the author of C# 7.0 in a Nutshell, this book covers the entire C# 6.0 language, including: All of C#'s fundamentals Advanced topics such as operator overloading, type constraints, ...
1.3 Implementing Our Algorithm in C++.... . . . 10 1.4 Software Engineering: Style...... . . 12 1.5 Common Programming Errors...... 13 1.6 Writing and Running a C++ Program.... . . . 14 1.6.1 ...
D.1 Increment operator with bool operand . 1185 D.2 static keyword . 1185 D.3 Access declarations 1185 D.4 Implicit conversion from const strings . 1185 D.5 register keyword 1185 D.6 C standard ...
4.4 The C++ memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.5 The C++ object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...
1.7 The C++ memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.8 The C++ object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...
type parameters),如圖 8。 template <typename T> class Rect : public Shape 侯捷觀點 JDK 1.5的泛型實現(Generics in JDK 1.5)7 { private: T m_left, m_top, m_width, m_height; ...
Operator Overloading Example: The Vector Struct 146 Which Operators Can You Overload? 153 User-Defined Casts 154 Implementing User-Defined Casts 155 Multiple Casting 161 Summary 165 Chapter 6: ...
Arrays in Java .............................. 44 You never need to destroy an object .................. 45 Scoping ........................................ 45 Scope of objects ...........................
1.2.8 Programming in Go......................................................................................10 1.2.9 Summary.............................................................................
1.2.8 Programming in Go......................................................................................10 1.2.9 Summary.............................................................................
Hungarian Notation........................55 Variable Naming .......Type and Constant Naming..............................................................57 Class Naming...............................