Conversion Operators)

Source: Internet
Author: User

When I was reading <windows core programming>, I saw a strange overload:

Operator proc *()

At that time, I didn't know much about C ++. I found this method confusing for a long time. There were no parameters, no returned values, and some types of destructor.

With a strange operator keyword, it must be related to operator overloading.

Later I learned that it is actually used for type conversion. It defines the behavior and result of this class when the forced type is converted to a certain type.

 Class  Con {  Public  :  Operator   Const   Char *() Const  {  Return  "  Hello World  "  ;} Con  VaR  ;  Const   Char * STR = ( Const   Char *) VaR  ; Cout <STR < Endl;  //  Hello world. 

 

This method is useful in some cases, but generally it is not used much.

I have never been too interested in these skills. I vaguely think that type conversion should be avoided in the object-oriented world. Isn't such a style different?

In short, the name is not authentic, do not use it.

A few days ago, I saw an article about the oldArticle: Http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758? Pgno = 3

This section describes the concepts and implementation of scopegard, which is widely referenced later.

The following section is written when the reference operation is implemented:Code:

 template  class  T>  class   refholder {T  &  ref _;   Public  : refholder (T  &  ref ): ref _ ( ref  ) {}  operator  T & ()  const  {  return   ref _ ;}; template   class  T>  inline refholder  
  
    byref (T & 
    T) { 
    return  refholder 
    
     (t) ;} 
   
  

 

This line:

 
OperatorT &()Const

Let me crack my head again while reading the code.
I suddenly remembered the reference-related things I saw when I looked at boost cmd_ptr not long ago. It was very vague and now it's on.
In some aspects, technologies are actually the same.
The skills on the other side may be infatuated, or mistaken, depending on who is using them.



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.