Invalid members in a nested class - WHY?

TTest= class public abc:Integer; type TTest2 = class procedure test; end; end; procedure TTest.TTest2.test(); begin abc:=5; //'Inaccessible here' end; 

I get "abc instance member unavailable here".

The situation is serious. I have to use a thread, but since TThread is abstract, a native class must inherit it. In my case, a class that needs a thread already inherits another class that cannot inherit TThread ... so this is not possible. My script is a nested class for inheriting TThread and below, I declare it. But Execute (); the method should use members of its parent class.

How can I do it? Why is this a mistake?

+4
source share
1 answer

The inner class is exactly this: a separate class. Only its namespace is nested inside the parent class. You can build an instance of an inner class without an instance of the parent class.

If you make the abc element of a class variable, you can access it from the inner class, because the vars class has global storage, independent of object instances.

Otherwise, you will need to pass the instance of the parent class to the nested class so that the nested class can write to the instance's parent fields.

+14
source

Source: https://habr.com/ru/post/1340883/


All Articles