is it possible to overwrite the = operator?
More specifically I am porting from C and I have the function below in a class. I’m not sure the equivalent in Xojo
Kact &operator=(const Kact &p)
{
id = p.id;
r = p.r;
c = p.c;
z = p.z;
return *this;
};
I’m awful with C, but this looks to me like in Xojo I’d have
obj1 = obj2
but rather than having obj1 be obj2, the two would end up with identical parameters.
Is this right?
@Tim Parnell our messages crossed.
I wrote the example above. It’s not a compare.
[quote=481919:@Chris Halford]@Tim Parnell our messages crossed.
I wrote the example above. It’s not a compare.[/quote]
Oh, that’s different. The way I do that usually is add a constructor that accepts a source instance and clone the properties. You can hard code it like your C example, or use introspection.
Something like
Constructor(oSource as MyClass)
and then
dim oThis as new MyClass(oinstanceToClone)
Tim, that’s what I thought too, but the object has a constructor as well in the C code.
What I’m afraid of is that in the millions of places I see = that I am reflecting what the original code expected.
The problem is that the logic of the code is way above my pay grade (mathematically) so I can’t just look and understand the actions to be clear on what the purpose is.
You could not use the constructor and create a method like CloneProperties. I’m not sure if that helps though.
Tim, it has that method, so I’m confused about why it also has this function. My fear is I’m misunderstanding the intention.
It sure looks like the same thing to me.
Maybe someone more intimate with C might have some answers. Ping some C folks perhaps?
Tim, I normally don’t run in those circles (?)
But I will do that next. Thanks.
[quote=481917:@Chris Halford]More specifically I am porting from C and I have the function below in a class. I’m not sure the equivalent in Xojo
Kact &operator=(const Kact &p)
{
id = p.id;
r = p.r;
c = p.c;
z = p.z;
return *this;
};
I’m awful with C, but this looks to me like in Xojo I’d have
obj1 = obj2
but rather than having obj1 be obj2, the two would end up with identical parameters.
Is this right?[/quote]
this is an assignment not comparison
C and C++ use == for comparisons
its why in C you often see things written as
if ( '1' == c )
as this will give you a compile error if you mistype it as
if ( '1' = c )
Looks like .Clone
or copy into
Kact &operator=(const Kact &p)
{
id = p.id;
r = p.r;
c = p.c;
z = p.z;
return *this;
};
this seems like it should be something like
Class Kact
sub operator_convert(p as Kact)
self.id = p.id
self.r = p.r
self.c = p.c
self.z = p.z
end sub
end class
Norman,
Then that would allow me to say
var myObject as new SomeClass
var SecondObject = myObject
and they would not just be pointing to the same object, but one would be a clone?
basically yes
would have to see how this class and this perator is used in the c code your porting but I expect this is more like “copy” than compare
the operator_convert doesn’t work as I thought it would in xojo.
It seems if I do an = when both source and destination are the same class, it just works like an assignment.
that may be a behaviour that cant be overridden
and there is no “operator_assign” as that is usually what operator_convert does
seems to be another case of “operator_convert is not called when you think it should be”
<https://xojo.com/issue/59553>
in this case assignment of a reference to another instance
in this case since it DOES seem to be a “clone” operation where you get 2 instances with the same data writing a “Clone” method might be the only alternative
That’s ok. What I think I understand from your message is that I should use do a “copy” function and use that.
I appreciate the input everybody. Thank you.
Kact &operator=(const Kact &p)
{
id = p.id;
r = p.r;
c = p.c;
z = p.z;
return *this;
};
tuns into
Function Clone( p as kAct) as kAct
dim tmp as new Kact
tmp.id = p.id
tmp.r = p.r
tmp.ic = p.c
tmp.iz = p.z
return tmp
end function
or make Clone a method on the class and then you get something simpler like
Function Clone() as kAct
dim tmp as new Kact
tmp.id = self.id
tmp.r = self.r
tmp.ic = self.c
tmp.iz = self.z
return tmp
end function