草庐IT

non-passphrase-protected

全部标签

c++ - 我应该公开 protected std::vector 吗?

我有一个类,我打算让其他人继承。它有一个std::vector,我只希望开发人员能够读取它,但不能修改它,我的基本函数会修改它。我应该提供一个返回const迭代器的函数,还是将vector公开为protected。谢谢 最佳答案 如果将vector公开为protected,子类将能够修改它。因此,您应该公开返回const迭代器的方法。您可以使用Non-VirtualInterfaceidiom为用户和子类公开不同的接口(interface)。 关于c++-我应该公开protectedst

c++ - 如何访问派生类中的 protected 成员?

来自http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.5Amember(eitherdatamemberormemberfunction)declaredinaprotectedsectionofaclasscanonlybeaccessedbymemberfunctionsandfriendsofthatclass,andbymemberfunctionsandfriendsofderivedclasses那么,如何访问派生类中的protected函数fun呢?#includeusingna

C++ 错误 : request for member '...' in 'grmanager' which is of non-class type 'GraphicsManager'

这个问题不太可能帮助任何future的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visitthehelpcenter.关闭10年前。我的类GraphicsManager出现错误。图形管理器.cpp:#include"C:\Users\ChrisUzzolina\Desktop\obj\include\GraphicsManager.h"#include#includeGraphicsManager::GraphicsManager(intSCREEN_WIDTH,intSCREEN_

c++ - protected 析构函数的基本原理

我注意到许多Poco类都有一个protected析构函数。这让他们编码起来更烦人。例如,这是我的一些代码:structW2:Poco::Util::WinRegistryConfiguration{typedefPoco::Util::WinRegistryConfigurationinherited;usinginherited::inherited;};std::stringget_documents_folder(){W2regc{"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\

c++ - const ref lvalue to non-const func return value 是否专门减少拷贝?

我遇到了一个C++习惯,我试图研究它以了解它的影响并验证它的用法。但我似乎找不到确切的答案。std::vectorgetThings();voiddo(){conststd::vector&things=getThings();}这里我们有一些返回非const&值的函数。我看到的习惯是在分配函数的返回值时使用const&左值。提出这个习惯的原因是它减少了拷贝。现在我一直在研究RVO(返回值优化)、复制省略和C++11移动语义。我意识到给定的编译器可以选择阻止通过RVO进行复制,不管这里是否使用了const&。但是,在防止复制方面,const&左值的使用对非const&返回值有任何影响吗

c++ - eclipse c++ 中的 "control reaches end of non-void function"警告但没有编译或运行时错误

这是我的代码:Composer&Database::GetComposer(stringin_last_name){for(inti=0;i想法是遍历Composer对象数组并返回对其last_name字段与“in_last_name”匹配的对象的引用。我明白警告在告诉我什么,即函数可能不会返回任何内容(如果用户提供了无效的姓氏)。我的问题是,我怎样才能避免这种情况?我尝试在for循环之后添加“return0”和“returnNULL”,但它无法编译。如果此方法什么也没找到,是否应该抛出异常? 最佳答案 您的函数被声明为返回一个Co

c++ - 将其中一个继承的 protected 成员设为私有(private)

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。

c++ - 具有基于 protected 基类方法的返回类型的函数

我试图让doSomeMethod()的返回类型与operator()相同在基类中,但如果它被声明为protected,编译器将拒绝带有error:notypenamed'type'in'std::result_of'的代码.如果它是公开的,它就可以工作,但我想知道从那以后我是否也可以让它为protected案例工作。这是重现错误的简单代码。#includeclassbase{protected:intoperator()(){return1;};};classchild:privatebase{autostaticdoSomeMethod()->typenamestd::result_

c++ - 铿锵错误 : non-const lvalue reference cannot bind to incompatible temporary

我有一段代码可以在MSVC上正常工作,但无法用clang++编译voidMyCass::someMethod(){std::wstringkey(...);auto&refInstance=m_map.find(key);//errorhere}其中m_map定义为std::map>m_map;和clang提示non-constlvaluereferencecannotbindtoincompatibletemporary我有点了解正在创建一个临时文件,但不确定如何解决这个问题。有什么想法吗? 最佳答案 右值不能绑定(bind)到非

C++ 保护 : fail to access base's protected member from within derived class

不可否认,这个问题的标题听起来与你的邻居迈克反复问的问题几乎一模一样。我发现很多问题的措辞相同,但没有一个是我的问题。首先,对于这个问题的上下文,我想澄清几点:1,c++访问控制是基于类而不是基于实例。因此,下面的代码是完全有效的。classBase{protected:intb_;public:boolIsEqual(constBase&another)const{returnanother.b_==b_;//accessanotherinstance'sprotectedmember}};2,我完全理解为什么以下代码无效-另一个可以是兄弟实例。classDerived:public