此代码过去适用于VisualStudio2015,但不再适用于VisualStudio2015update1。classFoo{protected:virtual~Foo(){};friendclassFoo__init;};classFoo__init{public:Foo_init;};staticFoo__init_Foo_init;失败并出现以下错误:ErrorC2248'Foo::~Foo':cannotaccessprotectedmemberdeclaredinclass'Foo'这是编译器错误还是代码格式错误? 最佳答案
这是基类中声明的样子:protected:voidindexAll();voidcleanAll();在派生类中,以下不编译:indexAll();//OKconnect(&_timer,&QTimer::timeout,this,&FileIndex::indexAll);//ERRORconnect(&_timer,SIGNAL(timeout()),this,SLOT(indexAll()));//OK我想使用connect的第一个变体,因为它会进行一些编译时检查。为什么会返回错误:error:'voidFiles::FileIndex::indexAll()'isprotect
如果我有一个继承自另一个的类,并且只有这个类必须使用某个变量,哪种做法更好?要在基类中说变量是“protected”,还是让它私有(private)并给它一个protectedgetter?我听说过相互矛盾的事情。我的老师告诉我要始终使用getter,而其他人告诉我,在任何级别使用getter都会暴露糟糕的程序设计。真正的答案是什么?我觉得两者都是不合逻辑的极端。此外,如果getter和setter是糟糕的程序设计,这是为什么呢?是否有任何资源可以教我更多有关如何构建代码的信息? 最佳答案 除了读取值之外,您是否需要(或预计您将来需
我有一个组件类,它定义了一般情况下应该如何创建Component的静态模板方法:classComponent{protected:uint32_tid;Component(uint32_tid):id(id){}templatestaticT*createComponent(){//contentherenotrelevantreturnnewT(someParameter);}};然后是一个实现,例如一个Button。这个类的构造函数不能直接使用,而是有一个调用Component::createComponent模板函数的静态方法。classButton:publicComponen
我创建了一个类,我想强制任何试图构建对象的人使用unique_ptr。为此,我考虑声明构造函数protected并使用返回unique_ptr的friend函数。所以这是我想做的一个例子:templateclassA{public:friendstd::unique_ptr>CreateA(intmyarg);protected:A(intmyarg){}};templatestd::unique_ptr>CreateA(intmyarg){//SinceIdeclaredCreateAasafriendIthoughtI//wouldbeabletodothatreturnstd::
这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Differencebetweenprivate,publicandprotectedinheritanceinC++在C++中派生为protected或private有什么区别?我想不通,因为两者似乎都限制从派生类对象访问基类成员
#includeclassA{protected:voidfoo(){}};classB:publicA{public:voidbar(){std::cout这里我试图获取基类的protected成员函数的地址。我收到此错误。main.cpp:Inmemberfunction‘voidB::bar()’:main.cpp:5:error:‘voidA::foo()’isprotectedmain.cpp:13:error:withinthiscontextmake:***[all]Error1将foo更改为公共(public)工程。打印&B::foo也可以。能否请您解释一下为什么我们无
我想知道为什么下面的代码不能编译:classbase{protected:typedefvoid(base::*function_type)()const;voidfunction_impl()const{}//error:‘voidbase::function_impl()const’isprotected};classderived:publicbase{public:operatorfunction_type()const{returnboolean_test()==true?&base::function_impl:0;//error:withinthiscontext}pro
派生类可以在其ctor-initializer中调用protected基类构造函数,但仅限于其自己的基类子对象,而不能在其他地方调用:classBase{protected:Base(){}};classDerived:Base{Baseb;public:Derived():Base(),//OKb(){//errorBaseb2;//error}};标准对此有何规定?这是[class.protected]/1:AnadditionalaccesscheckbeyondthosedescribedearlierinClause11isappliedwhenanon-staticdata
考虑下面的例子classbase{protected:intx=5;int(base::*g);};classderived:publicbase{voiddeclare_value();derived();};voidderived::declare_value(){g=&base::x;}derived::derived():base(){}据了解,只有基类的friend和派生类可以访问基类的protected成员,但在上面的示例中,我得到以下错误"ErrorC2248'base::x':cannotaccessprotected在类中声明的成员但是当我添加以下行时friendcl