`
yaojingguo
  • 浏览: 201954 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Type Operator Overloading in C++

阅读更多

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;
}
 
分享到:
评论

相关推荐

    Programming.with.ANSI.Cplusplus.2nd.Edition.0198083963

    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, ...

    Practical C++ Programming C++编程实践

    : 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....

    Object.Oriented.Programming.with.Cplusplus.2nd.Edition.019

    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...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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 ...

    C++ for Programmers

    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 ...

    C# 7.0 Pocket Reference: Instant Help for C# 7.0 Programmers

    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, ...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    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 ...

    C++标准(Standard for Programming Language C++)

    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 ...

    Google C++ International Standard.pdf

    4.4 The C++ memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.5 The C++ object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    C++ 标准 ISO 14882-2011

    1.7 The C++ memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.8 The C++ object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    JDK 1.5的泛型實現(Generics in JDK 1.5)

    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; ...

    Professional C# 3rd Edition

    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: ...

    Thinking in Java 4th Edition

    Arrays in Java .............................. 44 You never need to destroy an object .................. 45 Scoping ........................................ 45 Scope of objects ...........................

    The way to go

    1.2.8 Programming in Go......................................................................................10 1.2.9 Summary.............................................................................

    [Go语言入门(含源码)] The Way to Go (with source code)

    1.2.8 Programming in Go......................................................................................10 1.2.9 Summary.............................................................................

    Tricks of the Windows video Game Programming---part1

    Hungarian Notation........................55 Variable Naming .......Type and Constant Naming..............................................................57 Class Naming...............................

Global site tag (gtag.js) - Google Analytics