Function overloading trouble....

General FreeBASIC programming questions.
Post Reply
Dr_D
Posts: 2451
Joined: May 27, 2005 4:59
Contact:

Function overloading trouble....

Post by Dr_D »

I looked at the example, but I don't get it. It's possible to add, subtract, multiply and divide udt declared variables against each other, right? I don't really understand how to make it work.

Code: Select all

Type Vector3D
    X as Single
    Y as Single
    Z as Single
End Type


Delcare Sub Vector overload(Vec as Vector3D)

Dim a, b, c As Vector3D


c = Vector a+b
Can someone show me how to do this right? I really appreciate the help. ;)

btw: Thanks V1c!!!
DrV
Site Admin
Posts: 2116
Joined: May 27, 2005 18:39
Location: Midwestern USA
Contact:

Post by DrV »

Nope, that's operator overloading (not implemented yet). Function overloading is implemented, though...
Dr_D
Posts: 2451
Joined: May 27, 2005 4:59
Contact:

Post by Dr_D »

Oops...

*slaps forehead*

So the function overloading is where we can send different variable types to functions, as long as they're declared?
dumbledore
Posts: 680
Joined: May 28, 2005 1:11
Contact:

Post by dumbledore »

yep :P
except you need to rewrite the function for every instance of a different variable type which you declare
jdebord
Posts: 547
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Post by jdebord »

Functions cannot return UDT's. You should use a subroutine instead:

Code: Select all

option explicit

Type Vector3D 
    X as Single 
    Y as Single 
    Z as Single 
End Type 

function AddVector(Vec1 as Vector3D, Vec2 as Vector3D, Sum as Vector3D)
  Sum.X = Vec1.X + Vec2.X
  Sum.Y = Vec1.Y + Vec2.Y
  Sum.Z = Vec1.Z + Vec2.Z
end function

Dim as Vector3D a = (1, 2, 3), b = (4, 5, 6), c

AddVector(a, b, c)

print "c = ("; c.X;","; c.Y; ","; c.Z; " )"

sleep
jofers
Posts: 1525
Joined: May 27, 2005 17:18

Post by jofers »

Actually, they can in the latest version:
"functions call now return structures (user defined types) too, only external functions could before"
jdebord
Posts: 547
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Post by jdebord »

Yes, it works!

Code: Select all

option explicit

type Vector3D
    X as single
    Y as single
    Z as single
end type

function AddVector(Vec1 as Vector3D, Vec2 as Vector3D) as Vector3D
  
  dim as Vector3D Result
  
  Result.X = Vec1.X + Vec2.X
  Result.Y = Vec1.Y + Vec2.Y
  Result.Z = Vec1.Z + Vec2.Z
  
  AddVector = Result
end function

dim as Vector3D A = (1, 2, 3), B = (4, 5, 6), C

C = AddVector(A, B)

print "c = ("; C.X; ","; C.Y; ","; C.Z; " )"

sleep
Wonderful! Now I can write all my complex functions this way ;-)
Post Reply