草庐IT

package-private

全部标签

c++ - C++ 类中的 private const 是多余的吗?

假设我有一个类,其中有一个我不想更改的成员变量。假设没有setter函数,将该变量设为私有(private)const与仅将变量设为私有(private)之间有什么区别吗?私有(private):classConstFoo{public:Foo(inta);virtual~Foo();intval(){returnval_;}private:intval_;}私有(private)常量:classConstFoo{public:Foo(inta);virtual~Foo();intval(){returnval_;}private:constintval_;}看起来两者之间没有区别,因

c++ - 是否有适合 'ownership-in-a-package' 的 'handles' 可用?

Handles除了指针之外还有适当的语义。所以对我来说这样的例子(从RuleofZero中提取):classmodule{public:explicitmodule(std::wstringconst&name):handle{::LoadLibrary(name.c_str()),&::FreeLibrary}{}//othermodulerelatedfunctionsgohereprivate:usingmodule_handle=std::unique_ptr;module_handlehandle;};使用unique_ptr作为句柄的“包中所有权”是一个不好的例子。首先,它

c++ - 如何在 MVVM Windows 应用商店应用程序中为私有(private) C++ 模型层编写 Visual Studio 单元测试?

我正在编写一个相当复杂的Windows应用商店应用程序。为了保持代码的可维护性,我想充分利用单元测试。到目前为止,我的解决方案包括以下内容:应用本身和View层的C#/XAML项目一个C++Windows应用商店项目,其中ViewModel层由托管的WinRT引用类组成,而模型层是直接的C++,并且完全隐藏在View层之外。用于测试ViewModel层的C#Windows应用商店单元测试项目。一个C++项目,用于执行一些DirectX渲染(View层的一部分,与本文无关)使用C#项目测试C++WinRTViewModel类工作正常。使用不同的语言并不重要,因为一切都是公开的并且使用Wi

c++ - 根据 ISO 2003,内部类可以访问外部的私有(private)成员

如ISOC++2003中所述§11.8Nestedclasses[class.access.nest]Themembersofanestedclasshavenospecialaccesstomembersofanenclosingclass,nortoclassesorfunctionsthathavegrantedfriendshiptoanenclosingclass;theusualaccessrules(clause11)shallbeobeyed.Themembersofanenclosingclasshavenospecialaccesstomembersofaneste

c++ - 在 friend 类模板的基本子句中访问私有(private)定义

在C++中,使类模板B的基类规范依赖于与类模板是friend的类A的私有(private)定义是否合法>B?示例代码:structEmpty{};templatestructB;structA{friendstructB;private:usingBase=Empty;};templatestructB:T::Base{};intmain(){Btest;return0;}Godbolt链接:https://godbolt.org/g/HFKaTQ代码在Clang主干(和旧版本)和MSVC19(VS2017)上编译良好,但在GCC主干(和旧版本)上编译失败:test.cpp:Inins

c++ - vector动态内存分配之private member vector

我是C++新手(我学过Fortran编程),我想为多维表动态分配内存。该表是私有(private)成员变量:classtheclass{public:voidsetdim(void);private:std::vector>thetable;}我想用函数setdim()设置表格的尺寸。voidtheclass::setdim(void){this->thetable.assign(1000,std::vector(2000));}我编译这个程序没有问题,但是当我执行它时,我遇到了一个段错误。对我来说奇怪的是,这段代码(见下文)完全符合我的要求,只是它没有使用我类的私有(private)

C++,VS2010 : Avoid downloading MS redistrubutable package. 静态链接?

VS2010能否将所有内容链接到所需的exe文件中?还是把MSVS2010的文件放到安装包里?我不希望用户考虑下载和安装某些东西。 最佳答案 在一个大型的MFC项目中尝试过为此使用静态链接,使用VS2010,我发现我仍然需要运行VS2010SP1redist包。该软件在很大程度上没有它也能正常工作,但用户会看到相当多的额外随机错误,这些错误似乎在安装redist后奇迹般地消失了。我猜它会更新运行时所依赖的其他Windows组件,但这只是一个猜测。我的建议是将它包含在您的安装中。 关于C+

c++ - 从非成员模板函数访问私有(private)内部类类型

考虑以下代码:#includeusingnamespacestd;classOuter{structInner{intnum;};public:staticInnerGetInner(){returnInner{-101};}};//voidfunc1(Outer::Innerinner){//[1]Doesnotcompileasexpected//coutvoidfunc2(Outer::Innerinner,Dummy=Dummy()){cout(Outer::GetInner());//[3]Howdoesthiscompile?//Outer::Innershouldnotb

c++ - Google Mock中如何访问私有(private)成员

我正在尝试为一个带有私有(private)vector的类编写模拟,它将数据插入到私有(private)vector中。但是,我没有看到使用GoogleMock来做到这一点的方法。理想情况下,我不希望在我的界面中有任何与测试相关的内容。此外,我不想让私有(private)vectorprotected并子类化该类并添加访问器方法,因为这会导致我的代码泄漏其实现。这是我目前所拥有的。我想要完成的是用Fake类插入数据,并使用Mock类在指向Fake类的指针上调用Real::first()(这样我就可以使用Fake的vector而不是Real的vector)。编译此程序时,返回-1而不是4

c++ - 公共(public)接口(interface)方法的私有(private)实现

这个问题在这里已经有了答案:OverridingpublicvirtualfunctionswithprivatefunctionsinC++(7个答案)关闭6年前。我遇到过一段代码,其方法通过公共(public)接口(interface)公开,而实现是私有(private)的。我不确定预期的行为应该是什么。简化示例:#includeclassInterface{public:virtual~Interface(){}virtualvoidmyIfMethod()=0;};classDerived:publicInterface{private:voidmyIfMethod(){std