Struct "inheritance"

Simen Kjærås simen.kjaras at gmail.com
Sat Feb 4 05:45:25 PST 2012


On Sat, 04 Feb 2012 13:55:55 +0100, Vidar Wahlberg <canidae at exent.net>  
wrote:

> On 2012-02-04 13:06, Simen Kjærås wrote:
>> It seems that what you want is alias this:
>
> Thank you both, that's exactly what I needed.
>
> Leeching a bit more on the thread:
> Going back to the method:
> int somethingNifty(Point p) {
>    return p.x + p.y;
> }
>
> Let's say I have the following code:
> for (x; 0 .. 10) {
>    for (y; 0 .. 10) {
>      Point p = {x, y};
>      somethingNifty(p);
>    }
> }
>
> [How] can you rewrite those two statements inside the loops to a single  
> line? For example (this doesn't work):
> somethingNifty(Point(x, y));

Like bearophile said, Point(x, y) should work - assuming you have
defined no other constructors for Point.

You can also explicitly define a constructor that does what you want.


> And finally a question about operator overloading, here's the code for  
> "Coordinate" ("Point" is similar in structure):
> import Point;
> struct Coordinate {
>    Point _point;
>    int _z;
>    @property auto point() const {
>      return _point;
>    }
>    @property auto point(Point point) {
>      return _point = point;
>    }
>    @property auto z() const {
>      return _z;
>    }
>    @property auto z(int z) {
>      return _z = z;
>    }
>    bool opEquals(ref const Coordinate c) const {
>      return z == c.z && point == c.point;
>    }
> }
>
> Compilation fails with the following error:
> Coordinate.d:18: Error: function Point.Point.opEquals (ref const  
> const(Point) p) const is not callable using argument types  
> (const(Point)) const
> Coordinate.d:18: Error: c.point() is not an lvalue
>
> Noteworthy the code compiles fine if i replace "point" and "c.point"  
> with "_point" and "c._point", but then I'm referencing _point directly  
> instead of through the property "point" (which may do something else  
> than just return _point in the future).
> I've looked at  
> http://www.d-programming-language.org/operatoroverloading.html#equals  
> and that page is slightly confusing. It claims that:
> If structs declare an opEquals member function, it should follow the  
> following form:
> struct S {
>    int opEquals(ref const S s) { ... }
> }
>
> However, I can't even get the code to compile if I do that, the compiler  
> (gdc-4.6) says:
> Error: function Coordinate.Coordinate.opEquals type signature should be  
> const bool(ref const(Coordinate)) not int(ref const const(Coordinate) c)
>
>
> I hope it's somewhat clear what I'm trying to achieve.

It is. The problem is that bool opEquals(ref const Point) expects a Point
by reference, and the return value from your property is a temporary, from
which we can get no reference. The solution is to remove the ref:
bool opEquals(const Point). The only reason to use ref here is to cut down
on copying, and might be worthwhile on larger structures.


More information about the Digitalmars-d-learn mailing list