草庐IT

protected

全部标签

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

c# - C#<->C++ DLLImport "Attempted to read or write protected memory."问题

我有一个C++dll,其中有一个函数,我试图从C#应用程序调用。这是C++头文件中的代码extern"C"_declspec(dllexport)intLabelStoringSSDsim(intdevNum,UCHARserial[40],UCHARwwn[40],UCHARConfigID[5],UCHARFrmRev[8],UCHARDevName[40],inteCode);这是C++源文件中的代码intLabelStoringSSDsim(intdevNum,UCHARserialLbl[40],UCHARwwnLbl[40],UCHARConfigID[5],UCHARFr

C++通过继承在具有 protected 构造函数的基类堆上分配对象

我有一个带有protected构造函数的类:classB{protected:B(){};};现在我从它派生并定义了两个静态函数,我设法实际创建类B的对象,但不是在堆上:classA:publicB{public:staticBcreateOnStack(){returnB();}//staticB*createOnHeap(){returnnewB;}//CompiletimeErroronVS2010};Bb=A::createOnStack();//ThisworksonVS2010!问题是:1)VS2010允许第一种情况是错误的吗?2)是否可以在不以任何方式修改B的情况下创建B

c++ - boost::bind protected 成员和上下文

在下面的代码中,有两个使用boost:bind表达式对std::for_each的“等效”调用。指示的行编译,指示的失败行失败。我能在标准中找到的最佳解释是“因为我们这么说”。我正在寻找“为什么标准表明这种行为”。我的假设如下。我的问题很简单:为什么指定的行可以编译而下面的等效行无法编译(我不想因为“标准是这样说的”,我已经知道了-我不会接受任何给出的答案这是一个解释;我想要一个关于为什么标准这样说的解释)。注:虽然我用的是boost,但是boost与本题无关,使用g++4.1.*和VC7.1复现了各种格式的错误。#include#include#include#includeclas

c++ - Eigen 错误 : please_protect_your_min_with_parentheses

我正在尝试通过运行包附带的测试代码来测试Eigen的非线性优化功能。我被这些错误困住了(更像是困惑):Error5errorC2039:'please_protect_your_min_with_parentheses':isnotamemberof'std::numeric_limits'c:\programfiles(x86)\microsoftsdks\windows\v7.0a\include\eigen-eigen-5097c01bcdc4\unsupported\eigen\src\nonlinearoptimization\lmpar.h184Error7errorC20

c++ - 在派生类中强制执行 protected 构造函数

是否有任何机制允许在派生类中强制执行protected构造函数?简单的例子:templateclassFactory;classBase{templatefriendclassFactory;protected:Base();};classChild:publicBase{public:Child();//thisshouldleadtoacompiletimeerror};classFactory{Base*GetNew(){BOOST_STATIC_ASSERT(boost::is_base_of::value);Base*b=newT();b->doStuff();returnb

c++ - 指向具有 protected 继承的基类方法的指针

我有这个代码:classFoo{public:intx=4;int&operator[](size_tindex){returnx;}};classBar:protectedFoo{public:usingFoo::operator[];Bar(){x++;}};intmain(intagrc,char**argv){typedefint&(Bar::*getOp)(size_tindex);Barb;autobVal=b[4];getOpo=&Bar::operator[];autobVal2=(b.*o)(7);}但是,我不能编译它,因为errorC2247:'Foo'notacc

c++ - 有 friend 看基类吗?

给定示例代码:classBase{public:boolpub;protected:boolprot;};classDerived:privateBase{friendclassMyFriend;};classMyFriend{Derived_derived;voidtest(){//Doesstandardprovidemeaccessto_derived.puband_derived.prot?cout作为好友,我是否可以像我作为好友的类中的成员函数一样获得所有访问权限?换句话说,因为我是friend,我可以获取私有(private)继承的基类的protected成员和公共(pub