草庐IT

rb_protect

全部标签

c++ - 派生类中 protected 成员函数地址不可访问

#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也可以。能否请您解释一下为什么我们无

C++ 错误 : base function is protected

我想知道为什么下面的代码不能编译: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

c++ - 访问基类的 protected 构造函数

派生类可以在其ctor-initializer中调用protected基类构造函数,但仅限于其自己的基类子对象,而不能在其他地方调用:classBase{protected:Base(){}};classDerived:Base{Baseb;public:Derived():Base(),//OKb(){//errorBaseb2;//error}};标准对此有何规定?这是[class.protected]/1:AnadditionalaccesscheckbeyondthosedescribedearlierinClause11isappliedwhenanon-staticdata

c++ - 派生类不能访问基类的 protected 成员

考虑下面的例子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

c++ - "temporary of type ' A ' has protected destructor", 但它的类型是 B

在以下代码中,使用Clang8.0.0+和-std=c++17编译,使用B{}创建派生类实例会报错错误:'A'类型的临时对象具有protected析构函数。当临时文件的类型为B(因此应该有一个公共(public)析构函数)时,为什么A会出现在此消息中?https://godbolt.org/z/uOzwYaclassA{protected:A()=default;~A()=default;};classB:publicA{//canalsoomitthese3lineswiththesameresultpublic:B()=default;~B()=default;};voidfoo(

c++ - 为什么允许在派生类中调用 protected 静态方法?

如解释的那样,不允许在派生类中调用protected构造函数here.接受的答案解释说protected仅当A类的对象是B类的子对象。到目前为止,还不错。但是,为什么允许(至少在GCC4.6.3中)调用静态保护方法?具体来说,以下编译对我来说没有任何意义,而注释行则没有:classA{protected:A(){}staticAmakeA(){returnA();}};classB:publicA{public:staticAmakeAFromB(){returnmakeA();//compiles//returnA();//doesnotcompile}};从哲学上讲,构造函数非常类

c++ - protected 继承

为什么定义和提出protected和private继承?我知道在某些情况下可以使用私有(private)继承,但不推荐这样做。protected继承怎么样?谁能给我提供一种选择protected继承的情况?我很少看到这个。非常感谢! 最佳答案 私有(private)继承通常用于mixin——人们继承是为了从基类中获取功能,而不是因为“is-a”继承。protected继承也可以用于混合,其中混合功能也可用于下游类。 关于c++-protected继承,我们在StackOverflow上找

c++ - 将 protected 析构函数虚拟化是否有用?

/*ChildisinheritedfromParent*/classParent{public:Parent()//Constructor{cout如果我将Parent的析构函数设为虚拟,则会出现错误,那么将protected析构函数设为虚拟的目的是什么? 最佳答案 举个例子:假设你有一个实现引用计数的基类。您有一个addRef和一个release方法,并且您希望销毁您的对象,如果(并且仅)内部计数器通过对release的调用。因此,首先您希望您的析构函数受到保护(因为您只想从release中销毁对象)。如果你打算从你的类派生,你

c++ - VS2015 更新 1 个错误,或错误的 C++ : Why can't a friend class access its friend's protected destructor?

以下似乎是ZeroCICE在其自动生成的代码中采用的一种模式,在我看来,这似乎是他们现在为其工具的许多版本制作单例(不确定为什么)的一种方式。各种编译器都没有问题,直到今天发现VisualStudio2015Update1(VS版本14.0.24720.00,VC++版本19.00.23506)报错。在Update1之前,VS2015也没有问题。我不确定它是带有Update1的VS2015C++编译器中的错误(回归?),还是其他编译器放任自流的错误(不符合标准)C++代码。这是代码模式的示例:classFoo{protected:virtual~Foo(){}friendclassFo

c++ - 为什么对于基于 RB 树的 C++ std::set 的插入时间基准,我得到的是常数而不是对数曲线?

我在HeapvsBinarySearchTree(BST)比较BST和Heap但是当我尝试对两者进行基准测试并比较结果时,我无法解释BST的数据。首先,我确认标准库确实使用了红黑树:WhatistheunderlyingdatastructureofaSTLsetinC++?然后我运行了这个基准测试。主要.cpp#include#include#include#includeintmain(intargc,char**argv){size_ti,n;std::setbst;std::random_devicedev;unsignedintseed=dev();std::mt19937p