在C ++中创建只读(public)类成员 [英] Creating read-only (public) class members in C++

查看:648
本文介绍了在C ++中创建只读(public)类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Actioncript 3等语言的背景,我们有一个特殊的方法来定义一个成员变量,作为一个实例和一个方法来设置/获取受保护或私有成员的值。让我举个例子:



在一个类中,我们可以这样说:

  private var _myString:String; 

public get myString():String
{
return _myString;
}

public set myString(newValue:String):void
{
//做一些超级秘密成员保护n'stuff
_myString = newValue ;
}

然后在对象之外,我可以执行以下操作:

  trace(myClass.myString); //输出任何_myString是。 (注意缺少()。它被访问像属性不是一个方法... 

进一步,我可以做删除public set myString方法,所以如果有人试图这样做我的类:

  myClass.myString =Something; //尝试分配 - 再次注意缺少()

它会抛出一个错误,让用户知道该属性是只读的。



现在,因为我使用C + +,它是无限更真棒比Actionscript 3,我想知道如何可以模仿这种类型的行为。我不想使用一堆脏 getVariable() setVariable ()方法,我希望通过一些操作符重载欺骗我可以做完全相同的事情可能在这里注意我是一个noob,所以请告诉我这样。)



更新
我想解释这是最简单的方法,我试图基本上有getters和setters,但通过赋值,而不是对于C ++,语法糖并不存在,这意味着,你必须自己实现get / set方法。
所以在你的case你将不得不实现一个getVariable()方法没有它的等效Set方法,以使它只读。



t使用宏,使它看起来像你有一些只读属性。这将只是在他们从现在起5年后读取你的代码时离开。


I'm coming from a background in languages like Actionscript 3 where we have a special way of defining a member variable as both an instance and a method for setting/fetching the value of a protected or private member. Let me give an example:

Within a class, we can say something like this:

private var _myString:String;

public get myString():String 
{
    return _myString;
}

public set myString(newValue:String):void
{
    //Do some super secret member protection n' stuff
    _myString = newValue;
}

And then outside of that object I can do the following:

trace(myClass.myString); //Output whatever _myString is. (note the lack of (). It's being accessed like property not a method...

And even further, I could do something like delete the "public set myString" method, so if someone tried to do this with my class:

myClass.myString = "Something"; //Try to assign - again note the lack of ()

It would throw an error, letting the user know that the property is available as read-only.

Now since I'm using C++ and it's infinitely more awesome than Actionscript 3, I'm wondering how I can mimic this type of behavior. I don't want to use a bunch of dirty getVariable() and setVariable() methods. I was hoping through some operator overloading trickery I could make the exact same thing possible here. Note I am a noob, so please address me as such. :)

Update I guess the easiest way to explain this is, I'm trying to essentially have getters and setters but invoke them through assignment rather than with the parentheses ().

解决方案

Sorry, with C++, the syntactic sugar is not there, which means you have to implement the get/set methods yourself. So in your case you will have to implement a getVariable() method without it's equivalent Set method in order to make it read only.

And please don't resort to macro's to make it Look like you have some read only properties. That will just piss people off when they read your code 5 years from now.

这篇关于在C ++中创建只读(public)类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆