www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Property assign

reply Zarathustra <adam.chrapkowski gmail.com> writes:
How to overload assing to property ?
___________________________________________________
enum A : ubyte{
    A1,
    A2
}

struct B{
    ubyte b1;	
    ubyte b2;	
    ubyte b3;

    void 
    opAssign (A a){
        b1 = a;
    }
}

class C{
    private:
    ubyte	_a;
    B 	_b;

    public:
    ubyte a(ubyte a_){ return _a = a_; }
    //  ubyte a(ubyte a_){ return _a = a_; } <- It works 
                                                            //   but it's
uncomfortable
    ubyte a(            ){ return _a        ; }

    B b(B b_){ return _b = b_; }
    B b(      ){ return _b        ; }
}

int
main(){
    B objB;
    C objC = new C;

    objB = A.A1;	// it's OK
    objC.b = A.A1;	// error :(

    objC.b = objB;	// it's OK
    objC.b = B(A.A1)    // it's OK
 
    return 0;
}
May 03 2008
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
Add this in class C:

B b(ubyte b_) {_b = b_;}

-Steve 
May 04 2008