c++ 试用 define private public 报错

尝试将private定义成public--报错----redeclared with different access

查阅材料:https://stackoverflow.com/questions/47839718/sstream-redeclared-with-public-access-compiler-error

I wanted to check whether Catch 2.1.0 fixed all perf. regression from Catch Classic on a real world test suite, but using #define private public in test files causes compilation errors because of ODR violations.。 

如果是UT中需要访问私有成员,可以采用下面开源库。

可以通过模板方式错位实现对私有成员的访问 https://github.com/martong/access_private-- The ISO C++ standard specifies that there is no access check in case of explicit template instantiations (C++14 / 14.7.2 para 12).

 

----update 

实际上gcc可以使用-fno-access-control进行单元测试。这样访问控制就失效掉了~~  参考了cpp-stub--https://blog.csdn.net/coolxv_6533/article/details/79550197

 

posted @ 2023-02-16 08:41  dzqabc  阅读(238)  评论(0编辑  收藏  举报