我是C++的新手,我有一个关于继承中的c++protected和private成员的问题。如果一个类是public继承了一个基类,protected和private成员变量是否会成为派生类的一部分?例如:classBase{protected:inta;intb;private:intc;intd;public;intq;};classDerived:publicBase{};类Derived是否也有a,b,c,d,q的所有成员?我们可以在Derived类中将inta定义为public、protected和private吗? 最佳答案
关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。关闭7年前。Improvethisquestion我正在寻找一个C++和开源库来保护商业软件再次破解等...你认识一个吗?
当我在一个类中声明一个protected数据成员时,这意味着它不能被外部世界访问,只能被派生类访问。我的问题是willitbeaccesibletoaclassthatisderivedfromthederivedclass? 最佳答案 是的,protected数据成员在继承层次结构中一直是可访问的。通常最好避免使用protected数据。另一种方法是编写访问私有(private)数据的protected方法。这使数据封装在一个类中。它还可以轻松地为数据更改设置断点。 关于c++-pro
我有一个类,我打算让其他人继承。它有一个std::vector,我只希望开发人员能够读取它,但不能修改它,我的基本函数会修改它。我应该提供一个返回const迭代器的函数,还是将vector公开为protected。谢谢 最佳答案 如果将vector公开为protected,子类将能够修改它。因此,您应该公开返回const迭代器的方法。您可以使用Non-VirtualInterfaceidiom为用户和子类公开不同的接口(interface)。 关于c++-我应该公开protectedst
来自http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.5Amember(eitherdatamemberormemberfunction)declaredinaprotectedsectionofaclasscanonlybeaccessedbymemberfunctionsandfriendsofthatclass,andbymemberfunctionsandfriendsofderivedclasses那么,如何访问派生类中的protected函数fun呢?#includeusingna
我注意到许多Poco类都有一个protected析构函数。这让他们编码起来更烦人。例如,这是我的一些代码:structW2:Poco::Util::WinRegistryConfiguration{typedefPoco::Util::WinRegistryConfigurationinherited;usinginherited::inherited;};std::stringget_documents_folder(){W2regc{"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\
我正在尝试从模板中公开派生一个类,这将使它从基类继承并获得对protected成员的访问权限。但是在模板展开之前它没有这些权限,所以它不能使用Base成员作为模板参数:usingFun=void(*)();classBase{protected://friendclassDerived;//...needthistoeliminatecomplaintstaticvoidsomething();};templateclassVariant:publicBase{};classDerived:publicVariant{//`something()`isprotectedpublic:v
classBase{protected:voidfunc1();};classDerived:publicBase{friendclassThird;};classThird{voidfoo(){Derived;d.func1();}};我可以在VC14(VisualStudio2015)中编译代码而不会出错但从VC12(VisualStudio2013)得到错误cannotaccessprotectedmemberdeclaredinclass'Base'谁是对的?这种具有继承性的友元的正确性是什么?来自MSDNhttps://msdn.microsoft.com/en-us/lib
与thisquestionaboutstaticinitializers不同但可能相关.前两个函数编译良好,最后一个函数在vc++中不编译,但在clang和gcc中编译:classA{protected:std::stringprotected_member="yay";public:voidwithNormalBlock();voidwithFunctionBlock();voidnoLambda();};voidA::withNormalBlock(){try{throwstd::exception();}catch(...){[this](){std::coutinclang(好
classA{protected:intm_a;intm_b;};classB:publicA{};在B类中,我想将m_a设为私有(private)。下面的做法是否正确classB:publicA{private:intm_a;};这不会产生2个m_a拷贝吗? 最佳答案 调整成员访问控制的正确方法是使用usingdeclaration:classB:publicA{private:usingA::m_a;}只写intm_a;确实会导致m_a的两个拷贝,并且派生类将能够访问A的通过编写A::m_a复制m_a。