How do I explicitly access the defualt LET operator after defining a custom LET

General FreeBASIC programming questions.
Post Reply
Manpcnin
Posts: 46
Joined: Feb 25, 2007 1:43
Location: N.Z.

How do I explicitly access the defualt LET operator after defining a custom LET

Post by Manpcnin »

So I need to deep copy a UDT with a lot of shallow fields and the cleanest way would be something like:

Code: Select all

operator MyType.let(byref RHS as MyType)
	base.let this = RHS
	'deep copy stuff that actually needs it here.
end operator
Obviously this is only valid if mytype extends an object, (which is fair) and if its super type already implements let (Which doesn't help anything, because I'd still have to implement it, and seems weird, because UDTs & objects already have a default let operator)

Anyway the point is: How do I explicitly access the default LET operator.

Without it that code would just infinitely recurse.

'--------Edit--------

The messy ways that I'm trying to avoid are:

Code: Select all

.field1 = RHS.feild1
'...
.fieldn = RHS.feildn 'Breaks as soon as one adds extra fields
'deep copy stuff that actually needs it here.
or

Code: Select all

offset =  @RHS - @this
for p as ubyte ptr = @this to (@this)+sizeof(MyType)-1
    *p = *(p+offset)
next p 'Do I really need to explain why I don't want to do this?
'deep copy stuff that actually needs it here.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: How do I explicitly access the defualt LET operator after defining a custom LET

Post by caseih »

In C++ I've often done the memcpy thing which you dismissed as messy. The only other option is to just assign each field individually, which as you say can get long and tedious (and fragile if you change things). There's no way I know of to call the generic LET inside a LET operator override function in any of the languages I'm familiar with. Perhaps an operator override isn't what you want. Maybe a dedicated method function might be cleaner, say MyType.copy().
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How do I explicitly access the defualt LET operator after defining a custom LET

Post by fxm »

From fbc 1.08 you can also use the new keyword Fb_Memcopy which has an even simpler syntax than memcpy:

Dim As UDT u1, u2

'.....
'.....

Fb_Memcopy(u2, u1, SizeOf(UDT)) '' shallow copy from u1 to u2, from fbc 1.08 only

' or

#include "crt/string.bi"
memcpy(@u2, @u1, SizeOf(UDT)) '' shallow copy from u1 to u2
Last edited by fxm on Oct 22, 2020 7:10, edited 1 time in total.
Manpcnin
Posts: 46
Joined: Feb 25, 2007 1:43
Location: N.Z.

Re: How do I explicitly access the defualt LET operator after defining a custom LET

Post by Manpcnin »

Nice! I'll Look forward to Fb_Memcopy in 1.08. I'd assumed it would be a while before it was release ready.
I guess I'll use memcpy for now. It uses the native word size & REP internally right? I remember it being very efficient.
Post Reply