草庐IT

protected

全部标签

c++ - protected 成员在派生类中不可访问

为什么基类中的protected成员在派生类中无法访问?classClassA{public:intpublicmemberA;protected:intprotectedmemberA;private:intprivatememberA;ClassA();};classClassB:publicClassA{};intmain(){ClassBb;b.protectedmemberA;//thissaysitisnotaccesible,violation?//.....} 最佳答案 您可以访问protectedmemberAin

c++ - 当值为指针时尝试读取或写入 protected 内存

我有这个代码:typedefstruct{stringfName;stringstr;}t;//-------Otherfunctions------//voidBeginTh(){stringarg="yes";t*arglist;arglist=(t*)malloc(sizeof(t));arglist->fName="comBomber";arglist->str=arg;_beginthread(startOver,0,(void*)arglist);free(arglist);}然后在'arglist->fName="comBomber";'我得到这个错误:Anunhandl

c++ - 使用 googletest 测试 protected 成员

我对谷歌测试时的继承感到困惑。我有一个具有protected属性的A类。如果我想访问那些我必须扩展那个类,但同时我还需要扩展public::testing::Test用于gtest的唯一目的.这个问题最优雅的解决方案是什么?我也试图避免#defineprotectedpublic 最佳答案 为避免在被测类中留下测试痕迹,请使用夹具的多重继承:classToBeTested{protected:boolSensitiveInternal(intp1,intp2);//Stillneedstesting}//Google-test:cl

C++ 性能/内存优化指南

有人有C++内存优化指南的资源吗?最佳实践、调整等?举个例子:Classxxx{public:xxx();virtual~xxx();protected:private:};由于此类中没有protected和私有(private)的项目,因此摆脱protected和私有(private)的编译器或内存分配会有任何好处吗?更新:程序员是做什么的:Classxxx{public:xxx();virtual~xxx();public:morestuff();more();ifndef__BUILD_WIN__public:evenmore();envenmore2();endifprotec

c++ - 如何防止派生类公开私有(private)/ protected 虚函数?

将所有虚函数构造为私有(private)或protected基类接口(interface)是有充分理由的(参见this)。但是,如何防止派生类(可能在外部客户手中)将私有(private)虚函数设为公共(public)呢?在VirtuallyYours,作者讨论了这个问题,但没有讨论解决方案。编辑:根据您的回答和我之前的想法,似乎没有办法阻止这种情况。但由于在这种情况下,很容易出错(客户端肯定会触及protected虚函数),编译器对这种用法发出警告是有道理的。我试着用g++测试它。首先,我写道:classA{protected:virtualvoidnone(){return;}};

c++ - 基本访问中的工厂方法在派生中 protected 构造函数

我希望从Initable派生的所有对象在销毁时调用terminate()。为此,我创建了一个带有自定义删除器的shared_ptr。我的问题是我无法访问派生类的protected构造函数,以便在Initable工厂方法中创建实例。应该保护构造函数,以防止在不使用工厂方法的情况下创建实例。classInitable{public:virtualvoidterminate()=0;templatestaticshared_ptrmake_initable(constTs&...args){returnshared_ptr(newT(std::forward(args)...),[](Ini

c++ - 具有多态性的多重保护继承

我有一个关于protected函数的多重继承和多态性的问题。很难描述它,所以我希望它足够清楚。假设我有三个类:classbaseClass{protected:virtualintfunction()=0;};classderived_A:publicbaseClass{intfunction(){//implementation1};};classderived_B:publicbaseClass{intfunction(){//implementation2};};classderived_C:publicderived_A,publicderived_B{baseClass**p

java - 包装 C++ 对象的最佳 JNI 模式?

我正在开发一个JavaAPI,其中许多Java对象实际上是等效C++对象的包装器。Java对象创建C++对象,并负责在不再需要它们时释放它们。我想知道为此使用的最佳模式,我可以看到两个可能的选项:使用静态native方法调用和最终变量来保存native句柄,在构造函数中构造C++对象。publicabstractclassNativeBackedObject1implementsjava.lang.AutoCloseable{protectedfinallong_nativeHandle;protectedfinalAtomicBoolean_nativeOwner;protected

c++ - C++ 头文件的 emacs 公共(public)/ protected /私有(private)标签缩进不适用于零偏移

即使我在我的.emacs文件中定义了一些东西,我也无法在emacs中为我的C++头文件获得零偏移量。下面的头文件显示了两个命名空间内的类定义,最重要的是我希望具有零偏移量的public关键字,如下所示。namespacen1{namespacen2//nooffset{classSomeClass//nooffsetfromnamespaceopencurly{public://thislinewithzerooffsetSomeClass();//offset4...};inlineSomeClass::SomeClass()//nooffset{}}//n2}//n2在我的.ema

c++ - 为什么 shared_ptr 可以访问 ingoring "protected access right"

我用shared_ptr做了一些测试,我想不出下面的问题。我刚开始学习boost库。有谁能告诉我原因吗?#include#includeclassA{public:virtualvoidsing(){std::coutpa(newB());pa->sing();deletestatic_cast(pa.get());deletepa.get();//thislinehasaproblemerrorC2248:“A::~A”:can'taccessprotectedmemmber(declaredinclass“A")return0;}intmain(){foo();return0;}但