草庐IT

Getter-Setter

全部标签

c++ - 简单的 C++ getter/setter

最近我将我的getter和setter编写为(注意:真正的类在getter/setter中做更多的事情):structA{constint&value()const{returnvalue_;}//getterint&value(){returnvalue_;}//getter/setterprivate:intvalue_;};它允许我执行以下操作:autoa=A{2};//non-constobjecta//createcopiesby"default"(valuealwaysreturnsaref!):intb=a.value();//b=2,isacopyofvalue:)au

c++ - 公共(public)变量访问的线程安全

我的类中有一些私有(private)变量(比如inta、intb、intc)。由于一些内部操作,我需要以线程安全的方式设置/获取此类变量,因此我使用了一些包装getter/setter并使用了作用域互斥体。voidsetA(inta){unique_locklock(opMutex);this->a=a;}voidgetA(inta){unique_locklock(opMutex);returna;}voidsetB(intb){unique_locklock(opMutex);this->b=b;}voidsetC(intc){unique_locklock(opMutex);th

c++ - 在 C++ 中创建只读(公共(public))类成员

我有Actionscript3等语言的背景,我们有一种特殊的方式将成员变量定义为实例和设置/获取protected或私有(private)成员值的方法。让我举个例子:在一个类中,我们可以这样说:privatevar_myString:String;publicgetmyString():String{return_myString;}publicsetmyString(newValue:String):void{//Dosomesupersecretmemberprotectionn'stuff_myString=newValue;}然后在该对象之外我可以执行以下操作:trace(my

c++ - 如何生成 setter 和 getter,并使用 C++ 预处理器定义 Q_PROPERTY()

我正在设计一个QT4类。到目前为止,我的类(class)是这样的:classGIHNode:publicQObject,publicQGraphicsItem{Q_OBJECTQ_INTERFACES(QGraphicsItem)[...]public:voidsetInteger(int);intgetInteger();[..]private:intInteger;Q_PROPERTY(intIntegerREADgetIntegerWRITEsetInteger)//DefinitionI'dliketoreplaceThesettersandgettersareimplemen

c++ - 这个 C++ setter/getter 模式破坏了什么?

在C++中使用GLSL语法我编写了自定义vector类,例如vec2、vec3等,它们模仿GLSL类型,大致如下所示:structvec3{inlinevec3(floatx,floaty,floatz):x(x),y(y),z(z){}union{floatx,r,s;};union{floaty,g,t;};union{floatz,b,p;};};vector上的操作是这样实现的:inlinevec3operator+(vec3a,vec3b){returnvec3(a.x+b.x,a.y+b.y,a.z+b.z);}这让我可以创建vector并使用类似GLSL的语法访问它们的组

c++ - Boost::variant 的多态 setter

我正在尝试将boost::variant与模板类型一起使用。例如,我有一个模板类型Tagboost::variantAnyTag包含Tag等类型,Tag和Tag.每个Tag有类型T的成员。现在,我想将这些变体放在一个容器中,并在运行时简单地分配值,例如,for(AnyTag&tag:AllTags){setValue(tag,getValueFromXml());}函数setValue(AnyTag&tag,T&val)必须使用AnyTag标签的运行时类型才能正确地为标签分配正确的值。我尝试解决这个问题可以在下面找到,它使用了另一个变体,它只包含可以在AnyTag(TagValueTy

c++ - 异常类中的 Setter

在异常类中使用setter成员函数可能意味着什么?使用setter的动机是有时在throw点没有足够的可用数据以便在catch点正确处理异常;因此在展开堆栈时必须添加附加信息。 最佳答案 查看Boost.Exception图书馆,最准确地说是thispage在标题为将任意数据添加到事件异常对象的段落中:voidparse_file(charconst*file_name){boost::shared_ptrf=file_open(file_name,"rb");assert(f);try{charbuf[1024];file_rea

c++ - 如何使用 const getter 对 std::set 进行排序

我有一个std::set容器,其元素是以下类的对象:classLaneConnector{public:constLane*getLaneFrom()const{returnFrom;}constLane*getLaneTo()const{returnTo;}private:Lane*From;Lane*To;}我的比较函数如下:structMyLaneConectorSorter{booloperator()(LaneConnector*c,LaneConnector*d){Lane*a=const_cast(c->getLaneFrom());Lane*b=const_cast(d

c++ - 在构造函数中使用 setter

我是一名尝试学习C++的Java开发人员。是否可以在构造函数中使用setter以重用setter提供的健全性检查?例如:#includeusingnamespacestd;classTest{private:intfoo;voidsetFoo(intfoo){if(foofoo=foo;}public:Test(intfoo){setFoo(foo);};}; 最佳答案 是的,建议这样做,基本上是因为你已经提到的原因。另一方面,您应该问问自己是否需要setter而不是直接在构造函数中实现检查。我写这篇文章的原因是setter通常会导

c++ - Getter 函数的 Const 正确性

这是一个关于const正确性的简单问题。我有这门课:templateclassFoo{public:std::mapmembers;templatestd::vector&member(conststd::string&memberName){returnboost::any_cast&>(members[memberName]);}};然后我有一个包含以下内容的仿函数:booloperator()(Foo&foo)const{std::vector&member=foo.member(_memberName);这里让我感到困惑的是我不能通过引用const来传递Foo,因为我正在调用非