Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating read-only (public) class members in C++
    primarykey
    data
    text
    <p>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:</p> <p>Within a class, we can say something like this:</p> <pre><code>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; } </code></pre> <p>And then outside of that object I can do the following:</p> <pre><code>trace(myClass.myString); //Output whatever _myString is. (note the lack of (). It's being accessed like property not a method... </code></pre> <p>And even further, I could do something like delete the "public set myString" method, so if someone tried to do this with my class:</p> <pre><code>myClass.myString = "Something"; //Try to assign - again note the lack of () </code></pre> <p>It would throw an error, letting the user know that the property is available as read-only.</p> <p>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 <code>getVariable()</code> and <code>setVariable()</code> 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. :)</p> <p><strong>Update</strong> 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 ().</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload