Home > Back-end >  Questions about the dynamically generated class instance
Questions about the dynamically generated class instance

Time:09-25

Roughly as follows:
Type
TTestClass=class (TObject)
Private
Fclass: TClass;
Public
Item: TObject;
The constructor create (aclass: TClass);

Implementation

The constructor TTestClass. Create (aclass: TClass);
The begin
Fclass:=aclass;
Item:=fclass. Create;
end;
Was meant to want to use this class, passing in different types, such as tstringbuilder, automatic generation of objects, but the results item are empty, tracking fclass. Create; Found is called tobject. Create, instead of tstringbuilder the create method, so there is no implementation, can you tell me how to implement this function

CodePudding user response:

Item:=fclass. Create; Of course not, so to:
Item:=aclass. Create;

CodePudding user response:

reference 1st floor DelphiGuy response:
item:=fclass. Create; Of course not, so to:
Item:=aclass. Create;

If this also not line

CodePudding user response:

(fClass. NewInstance). The Create ()

CodePudding user response:

The
reference 3 floor linuxpingwangping response:
(fClass. NewInstance). The Create ()

This seems to be also not line, before the create item is nil, the create, although the item is not nil, but the mouse moves up, is (), does not seem to be successful,

CodePudding user response:

refer to the second floor llmiss response:
Quote: refer to 1st floor DelphiGuy response:

Item:=fclass. Create; Of course not, so to:
Item:=aclass. Create;

Not like this


Yes,
Var
A: TTestClass;
The begin
A:=TTestClass. Create (TStringList);
Writeln (Anderson, tem. The ClassName);
end;

CodePudding user response:

reference 5 floor DelphiGuy reply:
Quote: refer to the second floor llmiss response:

Quote: refer to 1st floor DelphiGuy response:

Item:=fclass. Create; Of course not, so to:
Item:=aclass. Create;

Not like this


Yes,
Var
A: TTestClass;
The begin
A:=TTestClass. Create (TStringList);
Writeln (Anderson, tem. The ClassName);
end;


I use tstringbuilder tried it on, although the classname can show "tstringbuilder", but once the append, would capacity error list are out of bounds error, just as it had been created successfully

CodePudding user response:

refer to 6th floor llmiss response:
Quote: refer to the fifth floor DelphiGuy reply:

Quote: refer to the second floor llmiss response:

Quote: refer to 1st floor DelphiGuy response:

Item:=fclass. Create; Of course not, so to:
Item:=aclass. Create;

Not like this


Yes,
Var
A: TTestClass;
The begin
A:=TTestClass. Create (TStringList);
Writeln (Anderson, tem. The ClassName);
end;


I use tstringbuilder tried it on, although the classname can show "tstringbuilder", but once the append, would capacity error list are out of bounds error, just as it had been creating a successful

Use of the code is TStringbuilder (Anderson, tem) Append (' 122 ');

CodePudding user response:

I tried it on, do have a problem, you said is debugging found TStringBuilder. Create; No implementation (although it is true that the instance) is established, FMaxCapacity has not been initialized, or 0, a Append is beyond the limit, so it seems with the item:=fclass. Create; Directly create an instance is problematic (to initialize the class members of the class),

CodePudding user response:

refer to eight floor early play play nuclear response:
I tried it on, do have a problem, you said is debugging found TStringBuilder. Create; No implementation (although it is true that the instance) is established, FMaxCapacity has not been initialized, or 0, a Append is beyond the limit, so it seems with the item:=fclass. Create; Directly create an instance is problematic (to initialize the class members of the class),

That how to implement this function, the consult, because my class is directly inherited tobject, getclass and findclass may not be able to use

CodePudding user response:



references 9 f llmiss response:
Quote: refer to the eighth floor early play big play nuclear war reply:
I tried it on, do have a problem, you said is debugging found TStringBuilder. Create; No implementation (although it is true that the instance) is established, FMaxCapacity has not been initialized, or 0, a Append is beyond the limit, so it seems with the item:=fclass. Create; Directly create an instance is problematic (to initialize the class members of the class),

That how to implement this function, the consult, because my class is directly inherited tobject, may not be able to use getclass and findclass

Suddenly remembered that I've tested, if the incoming tstringbuilder, then fclass and tstringbuilder pointer address is the same, also said that actually fclass has pointed to the tstringbuilder VMT, according to the principle that should be able to realize, only direct fclass. Create, if the compiler cannot tell what I wanted to generate tobject instance or tstringbuilder instance, so he didn't succeed,

CodePudding user response:

Even if you use the item:=fclass. Create; When using the item or want to judge what is the class instance, and then cast or as in the operation, so when you create an instance of judgment is not a big problem, for example:
The constructor TTestClass. Create (aclass: TClass);
The begin
Inherited the Create;
Fclass:=aclass;
If aclass=TStringBuilder then
Item:=TStringBuilder. The Create
The else//other types one by one judge
Item:=aclass. Create;
end;

CodePudding user response:

Study a little, Item:=aclass. Create; This is actually called the TStringBuilder. NewInstance; To Create an instance (if the incoming is TStringBuilder), did not call TStringBuilder. Create;

CodePudding user response:

null
  • Related