Define the type Conversion Result of a class

Source: Internet
Author: User

Today, I read Objective C ++ and found a kind of skill that I have never understood. It is to define the result of a class after the type conversion. For example, I have a class like this:

class Sum2Int {    int a,b;public:    explicit sum2Int(int aa, int bb) : a(aa), b(bb) {}};

If I want it to be converted to an int, the result is a + B. What should I do? I used to define another member function in the class, such as get (), as follows:

class Sum2Int {    int a,b;public:    explicit Sum2Int(int aa, int bb) : a(aa), b(bb) {}    int get() {        return a+b;    }};

This can be achieved, but it is a little tedious. If there is a function: void print (int num), then when passing the parameter, I must write print (Sum2Int ). get ()). It is also advantageous that others know that this class has such a function that can return an int value. For more information about the new method, see the following code:

#include<iostream>using namespace std;class Sum2Int {    int a,b;public:    explicit Sum2Int(int aa, int bb) : a(aa), b(bb) {}    operator int() const {        return a+b;    }     operator double() const {        return 1.0*a + 1.0*b;    }    int get() {        return a+b;    }};void prt(int k) {    cout<<k<<endl;}void prt2(double k){     cout<<k<<endl;}int main(){    int a,b;    while(true)    {        cin>>a>>b;        prt(Sum2Int(a,b));        prt2(Sum2Int(a,b));        cout<<Sum2Int(a,b).get()<<endl;    }}

When passing parameters (I don't Know), the compiler will try to implement type conversion for the class. If the type conversion is overloaded, the conversion can be successful directly. What is the purpose of this operation? It's actually convenient ...... There are many resource management classes that will package resources and the resources themselves will be protected in the resource management class. However, sometimes you may need to use get () such functions may frequently use get () and other functions in some scenarios. Therefore, it is a good choice to reload type conversion ()

This method comes from article 15 of Objective C ++. This book is quite good, especially some programming ideas !" Feeling

If you have any questions, please point out ~!




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.