草庐IT

UAC-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++ - 如何防止派生类公开私有(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++ - 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;}但

c++ - protected 构造函数与纯虚拟析构函数

我需要建立一个类继承结构,其中抽象基类只包含成员变量(但没有成员方法)。成员方法将由派生类定义。因此,我需要的是这篇文章:Makingaclassabstractwithoutanypurevirtualmethods从前2个答案中,我意识到有两种方法可以实现它:使析构函数成为纯虚拟的。使构造函数受到保护。我很想知道这两种方法之间的区别。是否存在一种情况应该优先于另一种情况(或者可能是某些特殊情况,其中一种可以工作但另一种不行)?我想了想,也想不出什么。我在这里搜索了一些帖子的答案(Isthereauseformakingaprotecteddestructorvirtual?、C++

c++ - 无法从派生类访问基类中的 protected 成员

这是我的代码:#include#include#includeusingnamespacestd;classroot{protected:intsize;double*array;public:virtual~root(){}virtualroot*add(constroot&)=0;virtualroot*sub(constroot&)=0;virtualistream&in(istream&,root&)=0;virtualintgetSize()const=0;virtualvoidsetSize(int);};classaa:publicroot{public:aa();aa(

c++如何为同一成员创建公共(public)和 protected 访问器

如果我有两种方法-一种是公共(public)的,一种是protected返回对同一成员的引用,我会得到以下编译错误:'Server::getManager':cannotaccessprotectedmemberdeclaredinclass'Server'当我注释掉protected函数时,代码可以正常工作。你能告诉我为什么会这样吗?为什么编译器找不到相同成员的公共(public)函数?classManager{};classServer{public:constManager&getManager()const{returnm_man;}protected:Manager&getM