Copy Constructor vs Let Operator (Function Returns)

General FreeBASIC programming questions.
Post Reply
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Copy Constructor vs Let Operator (Function Returns)

Post by rolliebollocks »

if i use

Code: Select all

     return var
the function will call the copy constructor..

what will happen if i use

Code: Select all

    function = var
will it call the let operator instead?

Because this can get tricky when the default constructor is bypassed (which allots some amount of memory).

I was tricked by this.
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Copy Constructor vs Let Operator (Function Returns)

Post by dkl »

Yea, for Return, the result is constructed at every Return using the copy constructor or bitwise copy if there is none (I consider this latter part to be a bug, see also Copy construction behaviour).

On the other hand, for Function=, the result is constructed at the top of function using the default constructor (an error will be shown by recent fbc versions if there is none) and then each Function= does an assignment, using Let operator if it exists or bitwise copy.

Also, the compiler shows an error when mixing Return and Function= in the same function in such cases where the result is a UDT with constructors.
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Re: Copy Constructor vs Let Operator (Function Returns)

Post by rolliebollocks »

Thank you. That is good to know.
fxm
Moderator
Posts: 12577
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Copy Constructor vs Let Operator (Function Returns)

Post by fxm »

Obviously, when returning by reference, none constructor or let operator is called, both for 'Return' or 'Function ='.
fxm
Moderator
Posts: 12577
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Copy Constructor vs Let Operator (Function Returns)

Post by fxm »

These two operation cases are well summarized in documentation at KeyPgConstructor and at KeyPgOpLet.
Post Reply