From: White Wolf on
Erik wrote:
> White Wolf wrote:
>> You and Erik vehemently oppose me answering the OP. Because that
>> feeds the troll.
>
> Not at all. I admired the amount of work you put into that
> and I tried to express that.
>
> I was merely trying to let you know this guy's posting habits.

That's all right. My reply may help someone else, who really looks for
help. :)

--
BR, WW
From: n00m on
> Overload the assignment operator for the type of SomeField...


In Python we can do smth like this:
===========================================

class Foo:
def __init__(self, x, y):
self.x = x
self.y = y
def __setattr__(self, name, val):
if name == 'x':
self.__dict__[name] = val * 2
else:
self.__dict__[name] = val

===========================================

f = Foo(5, 8)
print '1:', f.x, f.y
f.x = 666
f.y = 111
print '2:', f.x, f.y

===========================================
1: 10 8
2: 1332 111
===========================================

How can it be done in C++?
Can we overload " = " for assignments to ***data*** members?
Any minimalistic sample..?

From: Alf P. Steinbach on
* n00m:
>
> In Python we can do smth like this:
> ===========================================
>
> class Foo:
> def __init__(self, x, y):
> self.x = x
> self.y = y
> def __setattr__(self, name, val):
> if name == 'x':
> self.__dict__[name] = val * 2
> else:
> self.__dict__[name] = val
>
> ===========================================
>
> f = Foo(5, 8)
> print '1:', f.x, f.y
> f.x = 666
> f.y = 111
> print '2:', f.x, f.y
>
> ===========================================
> 1: 10 8
> 2: 1332 111
> ===========================================
>
> How can it be done in C++?

Depends what you want to do.


> Can we overload " = " for assignments to ***data*** members?

Yes.


> Any minimalistic sample..?

#include <stdio.h>

class Foo
{
struct X
{
int value;
X(): value( 0 ) {}
void operator=( int v ) { value = 2*v; }
operator int() const { return value; }
};
public:
X x;
int y;

Foo( int xVal, int yVal )
{
x = xVal; y = yVal;
}
};

int main()
{
Foo f( 5, 8 );
printf( "1: f.x = %d, f.y = %d\n", (int)f.x, (int)f.y );
f.x = 666; f.y = 111;
printf( "2: f.x = %d, f.y = %d\n", (int)f.x, (int)f.y );
}

And you can get more advanced by returning proxy objects.

However it's neither good design nor (for that reason) idiomatic C++.

Idiomatic C++ is to not expose member variables for a class with any kind of
behavior, only for pure PODs (C like structures).


Cheers & hth.,

- Alf
From: n00m on

Thanks, Alf! I'll study your sample (surely it works :)).
From: Alf P. Steinbach on
* n00m:
> Thanks, Alf! I'll study your sample (surely it works :)).

Depends on your definition of "works".

As mentioned it's not idiomatic C++, the whole idea of exposing data members is bad.

But anyway, even in that context the code I presented was bad, just coding down
what was discussed earlier in the thread. When not listening to old senile
rockin' chair guys who can't help but mindlessly repeat & code down what they
hear, you should prefer to centralize things in constructors instead of fiddling
with assignment operations. Like,

#include <stdio.h>

class Foo
{
struct X
{
int value;
X( int v ): value( 2*v ) {}
operator int() const { return value; }
};
public:
X x;
int y;

Foo( int xVal, int yVal ): x( xVal ), y( yVal ) {}
};

int main()
{
Foo f( 5, 8 );
printf( "1: f.x = %d, f.y = %d\n", (int)f.x, (int)f.y );
f.x = 666; f.y = 111;
printf( "2: f.x = %d, f.y = %d\n", (int)f.x, (int)f.y );
}

Cheers & hth,

- Alf
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Iterators in reverse direction?
Next: Modular Programming