Break OO privacy: Difference between revisions

Added FreeBASIC
(Added F# version)
(Added FreeBASIC)
Line 428:
f1 print \ 50
</lang>
 
=={{header|FreeBASIC}}==
FreeBASIC generally does a good job of maintaining OO privacy as it doesn't support reflection and even its OffsetOf keyword cannot obtain the offset within a user defined type of a private or protected field. You therefore have to guess the offset of a non-public field in order to be able to access it using a raw pointer though this is not generally a difficult task.
 
However, as usual, macros come to the rescue and one can easily access non-public members by the simple expedient (or, if you prefer, 'dirty hack') of redefining the Private and Protected keywords to mean Public:
<lang freebasic>'FB 1.05.0 Win64
 
#Undef Private
#Undef Protected
#Define Private Public
#Define Protected Public
 
Type MyType
Public :
x As Integer = 1
Protected :
y As Integer = 2
Private :
z As Integer = 3
End Type
 
Dim mt As MyType
Print mt.x, mt.y, mt.z
Print
Print "Press any key to quit"
Sleep</lang>
 
{{out}}
<pre>
1 2 3
</pre>
 
=={{header|Go}}==
9,490

edits