c++ 结构体中operator int()方法是什么意思?
在boost.DI上看到一个结构体写法以前没有见过,这个结构体中的方法是什么意思?
struct width { int value;
constexpr operator int() const { return value; }
};
struct height {
int value;
constexpr operator int() const { return value; }
};
class button {
public:
button(width, height); // span constructor interface
};
button{width{10}, height{15}};
回答:
类型转换
#include <iostream>struct width{
int w;
constexpr operator int()const {
return w;
}
};
int main(void)
{
width w{8};
int i = w; // 隐式转换
int j = int(w); // 显式
int k = width(w);
std::cout<<i<<j<<k;
}
---
888
附:参考链接
以上是 c++ 结构体中operator int()方法是什么意思? 的全部内容, 来源链接: www.h5w3.com/192237.html