https://github.com/QuantumLeaps/OOP-in- ... on/shape.h
It's a const pointer to a struct. With declaration like this:
declare sub Shape_ctor(byval me as const Shape ptr, byval x as long, byval y as long)
Passing as a pointer byval is real troublesome. It's impossible to just translate as is from the C code:
Dim as Shape s1, s2
Shape_ctor(@s1, 0, 1)
The compiler will not stop complaining about Array not dimensioned.
Changing this to pass byref solved part of the problems. But now we encountered const:
declare sub Shape_ctor(byref me as const Shape, byval x as long, byval y as long)
In shape.bas (translated from shape.c) if you use me.x = x the compiler will complain because me is const. The only way is to remove const:
declare sub Shape_ctor(byref me as Shape, byval x as long, byval y as long)
But this will lose the protection of const.
I know FB supports OOP. I just wanted to translate the C code. Thanks.
How to translate this piece of code to FB?
-
- Posts: 65
- Joined: Jul 01, 2021 7:53
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: How to translate this piece of code to FB?
To protect the pointer the Const has to be put before the Ptr:
while translates to and protects both the content of the struct and the pointer.
BTW: int16_t is Short, not Long
Code: Select all
Type shape
x As Short '/* x-coordinate of Shape's position */
y As Short ' /* y-coordinate of Shape's position */
End Type
Declare Sub Shape_ctor(ByVal me As Shape Const Ptr, ByVal x As Short, ByVal y As Short)
Dim As Shape s1, s2
Shape_ctor(@s1, 0, 1)
Sub Shape_ctor(ByVal me As Shape Const Ptr, ByVal x As Short, ByVal y As Short)
Print me
'me = 0 'throws compiler error if uncommented
Print me
Print x
Print y
End Sub
Sleep
Code: Select all
int16_t Shape_getX(Shape const * const me);
Code: Select all
Declare Function Shape_getX(me As Const Shape Const Ptr) As Short
BTW: int16_t is Short, not Long
Last edited by grindstone on Jul 31, 2021 18:25, edited 1 time in total.
-
- Posts: 65
- Joined: Jul 01, 2021 7:53
Re: How to translate this piece of code to FB?
What is the point of this, my friend? Shape_ctor is used to emulate a constructor, you set it to print the value of the attributes into the screen so it's useless!
I already have working translation my friend. The only thing I lack is I have to strip the const otherwise it will not work.
shape.bi
shape.bas
main.bas
BTW, did you even try to compile your code to see if it could pass the compiler at all? I'm pretty sure I tried everything possible with passing by value a pointer but the compiler will not let me go any further and constantly flooding me with Array not dimensioned error.
I already have working translation my friend. The only thing I lack is I have to strip the const otherwise it will not work.
shape.bi
Code: Select all
#pragma once
type Shape
as integer x
as integer y
end type
declare sub Shape_ctor(byref me as Shape, byval x as integer, byval y as integer)
declare sub Shape_moveby(byref me as Shape, byval dx as integer, byval dy as integer)
declare function Shape_getX(byref me as Shape) as integer
declare function Shape_getY(byref me as Shape) as integer
Code: Select all
#include once "shape.bi"
sub Shape_ctor(byref me as Shape, byval x as integer, byval y as integer)
me.x = x
me.y = y
end sub
sub Shape_moveby(byref me as Shape, byval dx as integer, byval dy as integer)
me.x += dx
me.y += dy
end sub
function Shape_getX(byref me as Shape) as integer
return me.x
end function
function Shape_getY(byref me as Shape) as integer
return me.y
end function
Code: Select all
#include once "shape.bi"
#lang "fblite"
dim as Shape s1, s2
Shape_ctor(s1, 0, 1)
Shape_ctor(s2, -1, 2)
print "Shape s1(x = "; Shape_getX(s1); ", y = "; Shape_getY(s1); ")"
print "Shape s2(x = "; Shape_getX(s2); ", y = "; Shape_getY(s2); ")"
Shape_moveBy(s1, 2, -4)
Shape_moveBy(s2, 1, -2)
print "Shape s1(x = "; Shape_getX(s1); ", y = "; Shape_getY(s1); ")"
print "Shape s2(x = "; Shape_getX(s2); ", y = "; Shape_getY(s2); ")"
sleep
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: How to translate this piece of code to FB?
My intention was to show how the Const keyword works in FB. And because your shape.bi isn't available here, I wrote that litte example sub.hungnguyengia wrote:What is the point of this, my friend? Shape_ctor is used to emulate a constructor, you set it to print the value of the attributes into the screen so it's useless!
And yes, I compiled that snippet here, and it works.
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: How to translate this piece of code to FB?
Alright, is it this what you wanted?
shape.bi
shape.bas
main.bas
shape.bi
Code: Select all
#Pragma Once
Type Shape
As Integer x
As Integer y
End Type
Declare Sub Shape_ctor(me As Shape Const Ptr, ByVal x As Integer, ByVal y As Integer)
Declare Sub Shape_moveby(me As Shape Const Ptr, ByVal dx As Integer, ByVal dy As Integer)
Declare Function Shape_getX(me As Const Shape Const Ptr) As Integer
Declare Function Shape_getY(me As Const Shape Const Ptr) As Integer
Code: Select all
#Include Once "shape.bi"
Sub Shape_ctor(me As Shape Const Ptr, ByVal x As Integer, ByVal y As Integer)
me->x = x
me->y = y
End Sub
Sub Shape_moveby(me As Shape Const Ptr, ByVal dx As Integer, ByVal dy As Integer)
me->x += dx
me->y += dy
End Sub
Function Shape_getX(me As Const Shape Const Ptr) As Integer
Return me->x
End Function
Function Shape_getY(me As Const Shape Const Ptr) As Integer
Return me->y
End Function
Code: Select all
#Include Once "shape.bas"
#Lang "fblite"
Dim As Shape s1, s2
Shape_ctor(@s1, 0, 1)
Shape_ctor(@s2, -1, 2)
Print "Shape s1(x = "; Shape_getX(@s1); ", y = "; Shape_getY(@s1); ")"
Print "Shape s2(x = "; Shape_getX(@s2); ", y = "; Shape_getY(@s2); ")"
Shape_moveby(@s1, 2, -4)
Shape_moveby(@s2, 1, -2)
Print "Shape s1(x = "; Shape_getX(@s1); ", y = "; Shape_getY(@s1); ")"
Print "Shape s2(x = "; Shape_getX(@s2); ", y = "; Shape_getY(@s2); ")"
Sleep
Re: How to translate this piece of code to FB?
In c/c++ passing a struct to a sub has an overload, and is considered bad practise, so the pointer to the struct is passed as const so the address is not altered.
Note that fb defaults to passing a udt byref, so the same problem doesn't arise.
In fb the default is byval for a pointer anyway, so it not altered.
Even passing the pointer byref is OK in this example because you don't alter me the pointer address, you just alter the pointer value.
So Grindstones code could be cleared of const.
But of course the failsafe way is to use const because you might inadvertently change the address if the sub was convoluted and big.
Note that fb defaults to passing a udt byref, so the same problem doesn't arise.
In fb the default is byval for a pointer anyway, so it not altered.
Even passing the pointer byref is OK in this example because you don't alter me the pointer address, you just alter the pointer value.
So Grindstones code could be cleared of const.
But of course the failsafe way is to use const because you might inadvertently change the address if the sub was convoluted and big.
-
- Posts: 65
- Joined: Jul 01, 2021 7:53
Re: How to translate this piece of code to FB?
Yes, worked. Thank you, grindstone.