Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the int () method in operator

Shulou Source: shulou.com Published: 2022-06-02 09:54:17 09月17日 Update

This article will explain in detail how to use int() method in operator. The content of the article is of high quality, so Xiaobian shares it with you as a reference. I hope you have a certain understanding of relevant knowledge after reading this article.

operator int() is a type conversion operator, such as:

struct A

{

int a;

A(int i):a(i){}

operator int() const { return a; }

};

void main()

{

A aa(1);

int i = int(aa);

int j = aa; //same effect

}

The return value type of this function is the function name, so there is no need to express it explicitly.

What do you mean return type is function name?

============================

The return type is int, and the function name is int, that is, it is not written as int operator int() const { return value; },

The return type is omitted.

operator int() is a conversion operator, which allows this class to be used in place of an int. If an object of this type is used in a place where an int (or other numerical type) is expected, then this code will be used to get a value of the correct type.

For example:

int i(1);

INT I(2); // Initialised with constructor; I.a == 2

About how to use int() method in operator to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Tags: Type function that is method content article more knowledge article yes no function that is mass operator I.a reference help related operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information Huawei Docker Shulou Technology