Json icon indicating copy to clipboard operation
Json copied to clipboard

不太理解这个函数写法

Open KeepStudyingAhead opened this issue 3 years ago • 2 comments

Json Json::get_array_element(size_t index) const noexcept { Json ret; ret.v.reset(new json::Value(v->get_array_element(index))); return ret;}

此处获取数组元素为啥不像其他类型一样 return v->get_array_element(index) 呢? 而且修改后会报错: E0312 不存在用户定义的从 "const syo::json::Value" 到 "syo::Json" 的适当转换,

大佬能给我讲解一下吗

KeepStudyingAhead avatar Jun 01 '21 13:06 KeepStudyingAhead

@KeepStudyingAhead 这里用Pimpl手法把Json类的实现隐藏在json::Value类中,Json::get_array_element(index)通过调用实现类的get_array_element函数,但两个函数的返回类型不同,所以不能直接return v->get_array_element,要做一次转换。而其他像string、number等类型就可以直接返回。

Syopain avatar Jun 01 '21 14:06 Syopain

@KeepStudyingAhead 这里用Pimpl手法把Json类的实现隐藏在json::Value类中,Json::get_array_element(index)通过调用实现类的get_array_element函数,但两个函数的返回类型不同,所以不能直接return v->get_array_element,要做一次转换。而其他像string、number等类型就可以直接返回。

嗷嗷,又仔细看了看,确实返回值不同,我再去查查Pimpl手法,谢谢大佬!从tutorial过来的, 对接口设计确实比较生疏。

KeepStudyingAhead avatar Jun 01 '21 14:06 KeepStudyingAhead