Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Overloading opAssign() is like overloading the assignment operator in C++. It's for assigning to the object itself, not one of its members. It's really not going to do what you want. I believe that the closest that you're going to get is properties:</p> <pre><code>class Gerbil { public: @property int id() { return _id; } @property id(int newID) { //... Do whatever interception you want. _id = newID; } @property float x() { return _x; } @property x(float newX) { //... Do whatever interception you want. _x = newX; } @property float y() { return _y; } @property y(float newY) { //... Do whatever interception you want. _y = newY; } @property float z() { return _z; } @property z(float newZ) { //... Do whatever interception zou want. _z = newZ; } private: int _id; float _x, _y, _z; } </code></pre> <p><code>@property</code> enables property syntax so that you can use the function as if it were a variable. So,</p> <pre><code>//... auto copyOfGerbilX = gerbil.x; //translates to gerbil.x() gerbil.x = 9.0; //translates to gerbile.x(9.0) //... </code></pre> <p>is now legal even though <code>x</code> is a function rather than a variable. You can insert whatever special handling code you want in the functions. And because the syntax used to access the variables is just as if they were public member variables, you can freely refactor your code to switch between having them be properties or public member variables in your class definition (assuming that you haven't tried to do something like take their address, since that doesn't mean the same thing for a variable as a function).</p> <p>However, if what you're looking for is a generic way to not have to do all of those functions yourself, there is no direct construct for it. I believe that you could do it with compile-time reflection and string mixins or template mixins which would look at the list of your variables and then generate each of the property functions for you. However, then the extra handling code would have to be essentially the same for each function, and you'd have to be careful that the generated code was really what you wanted. I'm sure that it's feasible, but I'd have to work on the problem for a bit to produce a workable solution.</p> <p>To generate such code, you'd need to look at <a href="http://www.digitalmars.com/d/2.0/traits.html" rel="nofollow">__traits</a> and <a href="http://www.digitalmars.com/d/2.0/phobos/std_traits.html" rel="nofollow">std.traits</a> for the compile-time reflection and at <a href="http://www.digitalmars.com/d/2.0/template-mixin.html" rel="nofollow">template mixins</a> and <a href="http://www.digitalmars.com/d/2.0/statement.html#MixinStatement" rel="nofollow">string</a> <a href="http://www.digitalmars.com/d/2.0/expression.html#MixinExpression" rel="nofollow">mixins</a> for the code generation. I'd think twice about generating the code like that though rather than writing it by hand. It should be quite doable, but it won't necessarily be easy, debugging it could be entertaining, and if you're going to have to be fairly good with D templates and mixins to get it right.</p> <p>But essentially, what you're looking for is to use @property functions so that you can add your handler code and then possibly use compile-time reflection along with mixins to generate the code for you, but generating code like that is a fairly advanced technique, so you may want to wait to try that until you're more experienced with D.</p>
 

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