草庐IT

protected_member

全部标签

c++ - 具有混合继承修饰符的菱形继承(钻石问题)( protected /私有(private)/公共(public))

假设我们有类A、B、C、D,其中A是基础,B、C是介于两者之间,D是在菱形模型中派生的。注意:classB在private模式下继承virtualyclassA,C类在保护模式下继承虚拟A类。classA{public:intmember;//notethismember};classB:virtualprivateA//noteprivate{};classC:virtualprotectedA//noteprotected{};classD:publicB,//doesn'tmetterpublicorwhateverherepublicC{};intmain(){Dtest;te

c++ - 用这个技巧从外部访问 protected 成员,但这有效吗?

如果我有以下类(class):classFoo{protected:inti;public:Foo():i(42){}};当然,我无法从外部访问protected成员,但我可以做这个小技巧:首先我创建一个继承Foo的新类:classFoo2:publicFoo{public:intGetI(){returni;}};现在,只要我有一个Foo的实例或指向此类实例的指针,我就可以通过强制转换访问protected成员(因为我不使用任何其他成员):Foo*f=newFoo();Foof2;std::coutGetI()(f2)).GetI()我明白为什么会这样,但会不会有任何不良后果?编译器

c++ - 提供 protected 模板成员函数的公共(public)特化

我想做以下事情:classFoo{protected:templatevoidoperator()(constParam¶m){//stuffinvolvingsomeRTTImagic}public:voidoperator()(constA¶m)shouldbeoperator();voidoperator()(constB¶m)shouldbeoperator();}基本上,我有一个带有通用模板参数的通用operator()。但是,我只想发布类型安全的特定专业。谢谢! 最佳答案 只是给私有(privat

c++ - 为什么继承的 protected operator=() 具有公共(public)访问权限

声明为protected的重载运算符=对于继承父类作为public的子类是公开可访问的。#includeclassA{public:A(charc):i(c){}chari;protected:A&operator=(constA&rdm){std::cout编译时没有错误:$g++-Wall-otest_operator~/test_operator.cpp$./test_operatora.i==aaccessingoperator=()a.i==x直接使用A是编译不过的。operator=()以外的任何其他运算符重载都不会编译。使用g++4.4.7和7.3.0以及c++98和c+

C++:允许访问 protected 类成员而不是私有(private)成员

我知道您可以通过继承来做到这一点,但您应该在"is"情况下使用继承。我也知道有friend,但他们也允许访问私有(private)成员。有什么方法可以做到这一点(允许访问protected类成员而不是私有(private)成员)?为了改写这个问题,我有第1类和第2类。我希望第2类可以访问第1类的protected和公共(public)成员,但不能访问它的私有(private)成员。我该怎么做? 最佳答案 它并不优雅,但这可能适合你:classB;classA{protected:intx;private:inty;};classA_

c++ - Qt5 : error: 'WA_LockPortraitOrientation' is not a member of 'Qt'

我正在尝试将Qt4/Symbian项目编译为Qt5,同时保留对Qt4/Symbian的支持。目前MainWindow::setOrientation自动生成的样板函数给我带来了麻烦。它给我这些编译器错误:error:'WA_LockPortraitOrientation'isnotamemberof'Qt'error:'WA_LockLandscapeOrientation'isnotamemberof'Qt'error:'WA_AutoOrientation'isnotamemberof'Qt' 最佳答案 是的,正如您自己所说,这

c++ - 如何在 C++ 模板类之间共享 protected 成员?

考虑以下示例:class_ref{public:_ref(){}_ref(const_ref&that){}virtual~_ref()=0;};_ref::~_ref(){}templateclassref:public_ref{protected:ref(const_ref&that){}public:ref(){}ref(constref&that){}virtual~ref(){}templatereftryCast(){boolvalid;//performsomechecktomakesuretheconversionisvalidif(valid)returnref(*t

c++ - 错误 : imread is not a member of cv

我使用OpenCV3.0和Ubuntu14.04。我正在尝试使用opencv在ubuntu上编译一些代码。我收到错误"error:'imread'isnotamemberof'cv'"由于我以前的搜索知识,我尝试通过添加“highgui.h”进行编译。我使用:$g++main.cppHOG.cppHOGFeaturesOfBlock.cpp-I/usr/local/include/opencv-lml-lcvaux-highgui-lcv-lcxcore-ofeatureExtractor在终端上编译。有什么建议吗?问候。可以。 最佳答案

c++ - 错误 : no matching member function for call to 'push_back'

为什么我在最后两行收到错误?目标是在集合中找到对象,并修改其内容。usingnamespacestd;structmystruct{intid;vectory;mystruct(constintid):id(id){}booloperatorsx;mystructx(1);x.y.push_back(1);x.y.push_back(2);sx.insert(x);//set::iteratori=sx.find(1);constmystruct*x1=&(*i);constmystructx2=*x1;couty)y)y.push_back(4);}好像迭代器返回的是常量对象,不让我

c++ - 为什么纯虚拟/抽象类需要构造函数,特别是对于 protected const 成员变量?

我有一个这样定义的纯虚拟类:classBaseClass{protected:constintvar;public:voidsomefun()=0;//whatImeanbyapurelyvirtualclass//stuff...};如果我不添加这样定义的构造函数:BaseClass(constint&VAR):var(VAR){};我必须随后在派生类中使用,我的派生类无法将const变量var初始化为它想要的任何值。现在我真的明白这里发生了什么。在构造派生类之前,会调用基类的构造函数,此时必须初始化const成员变量。我的问题不是“我如何使我的代码工作”之类的问题,这已经完成了。我