I have this declaration:
Type
TmyObjA = class
private
FieldA: integer;
FieldB: integer;
public
...
end;
TmyObjB = class(TmyObjA)
private
FieldC: integer;
FieldD: integer;
public
...
end;
var MyObjB = TmyObjB
I want now to access the private field FieldA
of MyObjB
. I do like this :
TmyObjAPrivateAccess = class
public
FieldA: integer;
FieldB: integer;
end;
TmyObjAPrivateAccess(MyObjB).FieldA := something;
Does it will work or do I must do like this :
TmyObjBPrivateAccess = class
public
FieldA: integer;
FieldB: integer;
FieldC: integer;
FieldD: integer;
end;
TmyObjBPrivateAccess(MyObjB).FieldA := something;
NB: I do test, it's work in both variants, but it is not a demonstration, its just seem to work
Edit/Note
About why I need to access private member: The class I want to access private member is TTexture
:
TTexture = class(TInterfacedPersistent, ITextureAccess)
private
FWidth: Integer;
FHeight: Integer;
FPixelFormat: TPixelFormat;
FHandle: TTextureHandle;
FStyle: TTextureStyles;
FMagFilter: TTextureFilter;
FMinFilter: TTextureFilter;
FTextureScale: Single;
FRequireInitializeAfterLost: Boolean;
FBits: Pointer;
FContextLostId: Integer;
FContextResetId: Integer;
protected
public
property BytesPerPixel: Integer read GetBytesPerPixel;
property MinFilter: TTextureFilter read FMinFilter write SetMinFilter;
property MagFilter: TTextureFilter read FMagFilter write SetMagFilter;
property PixelFormat: TPixelFormat read FPixelFormat write SetPixelFormat;
property TextureScale: Single read FTextureScale; // hi resolution mode
property Style: TTextureStyles read FStyle write SetStyle;
property Width: Integer read FWidth write SetWidth;
property Height: Integer read FHeight write SetHeight;
property Handle: TTextureHandle read FHandle;
end;
This is my TmyObjA
.
Then I update this class to
TALPlanarTexture = class(TTexture)
private
FSecondTexture: TTexture;
FThirdTexture: TTexture;
FFormat: TALTextureFormat;
protected
....
end;
This is my TmyObjB
. But I have several other class like this one, like TALBiPlanarTexture = class(TALTexture)
, etc.. this why I want to have only one access class for all of them :
{************************}
{$IF CompilerVersion > 32} // tokyo
{$MESSAGE WARN 'Check if FMX.Types3D.TTexture still has the exact same fields and adjust the IFDEF'}
{$ENDIF}
TALTextureAccessPrivate = class(TInterfacedPersistent)
public
FWidth: Integer;
FHeight: Integer;
FPixelFormat: TPixelFormat;
FHandle: TTextureHandle;
FStyle: TTextureStyles;
FMagFilter: TTextureFilter;
FMinFilter: TTextureFilter;
FTextureScale: Single;
FRequireInitializeAfterLost: Boolean;
FBits: Pointer;
FContextLostId: Integer;
FContextResetId: Integer;
protected
public
end;
Now why I need to access private member of TTExture
? TTexture
is a quite simple class used by all OpenGL delphi framework. So replacing this class mean ... well mean redraw all the openGL firemonkey framework :) quite complicate job but I agree its a possibility for purist people who don't want to access private member.
I have another framework (video player) that on each video frame gave me one openGL texture ID with/height and scale. so on each new frame event, I need to update the Handle of my TTexture
plus the With
/height
of it and send it to the canvas.drawTexture (that only accept TTexture
)
Is there any way to update on each frame a TTexture
object with a given Handle
/width
/height
and scale
? simple anwer: no! yes their is a way, but i found it much more hazardous
than accessing directly private members. this way involve using ITextureAccess
to reset the handle to zero then call setwith
and setheight
that will finalize the texture then only after set back the handle, etc ...
but anyway my question was not about is it good or bad to access private members :)