0

we have this base class :

TCustomContextOpenGL = class(TContext3D)
  protected
    **class** procedure CreateSharedContext; virtual; abstract;
  end;

and in the program to know the current context class we do :

TContextManager.DefaultContextClass => return TContextClass = class of TContext3D;

that will for exemple return TCustomAndroidContext or TCustomContextIOS who override CreateSharedContext but let it protected

my problem is that i need to do

TContextManager.DefaultContextClass.CreateSharedContext 

But of course this will not work because CreateSharedContext is protected in TCustomContextOpenGL :( how can i do ?

zeus
  • 12,173
  • 9
  • 63
  • 184
  • http://stackoverflow.com/questions/18067430/accessing-protected-event-of-twincontrol – Andrei Galatyn Feb 06 '17 at 06:45
  • @AndreiGalatyn not really the same because here i speak about class not about object :( – zeus Feb 06 '17 at 08:36
  • This looks more like C++ than Delphi. In answer to your question you can't leave it as protected and access it - that is what protected is meant to prevent! So instead of leaving it protected, make the override public. The base class still can't be directly accessed but the descendant class can. – Dsm Feb 06 '17 at 08:44
  • Can't you use `RegisterContextClasses` and `UnRegisterContextClasses` – Dalija Prasnikar Feb 06 '17 at 09:55
  • yes i can, but look like overcomplicated to use registerContextClasses / UnRegisterContextClasses for the single line of code i want to do :( finally i end up by use {$ifdef android}....{$else ifdef ios}.... – zeus Feb 06 '17 at 10:07

1 Answers1

3

Best of all is to avoid direct call of protected method. If it is 3th party class and you can't change it, then you can access protected class methods same way as any other protected class member.

There is example how to access protected object events: Accessing protected event of TWinControl

Similar way you can access protected class methods:

type
  TCustomContextOpenGLHack = class(TCustomContextOpenGL);
  CCustomContextOpenGLHack = class of TCustomContextOpenGLHack;

procedure Test;
begin
  CCustomContextOpenGLHack(TContextManager.DefaultContextClass).CreateSharedContext;
end;
Community
  • 1
  • 1
Andrei Galatyn
  • 3,322
  • 2
  • 24
  • 38
  • 1
    it's will not work because TContextManager.DefaultContextClass return not an object but a class (ie: class of TContext3D) :( – zeus Feb 06 '17 at 10:05
  • @andray: aaah it's was simple like this ? :) ok thanks ! – zeus Feb 06 '17 at 12:22