-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
传值和传常量引用,在作为公共接口时有区别吗? #3
Labels
Comments
void hello(std::string* pstr); 会产生「跨模块内存管理」问题吗?——会!在跨模块中显式调用 ps 突然想起来 《Google C++ 编程规范》中硬性约定:
void Foo(const string& in, string* out); 不过,在论述为什么坚持上述约定时,未提到跨模块管理内存的梗,只是单纯地描述:
|
试验结果// 作为输入
_ADD_API void helloValue(std::string name); // ×
_ADD_API void helloRef(const std::string& name); // √
_ADD_API void helloPointer(const std::string* const name); // 〇 满足功能,比较丑而已
// 作为输出
_ADD_API std::string returnValue(void); // ×
_ADD_API std::string& returnRef(void); // 〇 功能不完善,需要保证引用源有效
_ADD_API bool returnPointer(std::string* out); // ×
_ADD_API bool returnSmartPtr(std::shared_ptr<std::string> out); // ×
_ADD_API bool returnSmartPtr2(std::shared_ptr<std::string>& out); // ×
_ADD_API bool returnSmartPtr3(const std::shared_ptr<std::string>& out); // ×
_ADD_API bool fillv(std::shared_ptr<std::vector<std::string>>& out); // ×
_ADD_API std::shared_ptr<std::vector<std::string>> returnv(); // × 结论
|
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
后者不会造成跨模块管理内存,前者呢?——运行时报错
__acrt_first_block == header
会产生「跨模块内存管理」问题吗?——会
返回引用呢?——不会,但需要注意引用本体的生存周期
The text was updated successfully, but these errors were encountered: