class WrappingInt32 {
private:
uint32_t _raw_value; //!< The raw 32-bit stored integer
public:
//! Construct from a raw 32-bit unsigned integer
explicit WrappingInt32(uint32_t raw_value) : _raw_value(raw_value) {}
uint32_t raw_value() const { return _raw_value; } //!< Access raw stored value
};
构造函数只能在类中的public成员中使用,其目的是为了初始化类的成员值(主要是private成员的值),构造函数必须和类同名,其没有返回值,加explicit是防止函数被隐式调用
隐式调用详情见:https://blog.csdn.net/gjggj/article/details/67627007?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-67627007-blog-116349808.pc_relevant_default&spm=1001.2101.3001.4242.1&utm_relevant_index=3
举个栗子:
class WrappingInt32{
private:
int _raw_value; //!< The raw 32-bit stored integer
public:
explicit WrappingInt32(int raw_value) :_raw_value(raw_value){};
int raw_value() const {
// cout<<_raw_value;
return _raw_value;
} //!< Access raw stored value
};
WrappingInt32 tmp=WrappingInt32(1);
// tmp.raw_value();
// cout<
输出结果: 1



