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 ().