Forum > General
[NOT Solved] specialize TFPGObjectList<T>
BrunoK:
Simplified project
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program pgmFgglTest; {$mode ObjFPC}{$H+} uses Classes, SysUtils, fgl; type { TSymbolBufferList - circular buffer list } TSymbolBufferList = class(specialize TFPGObjectList<TObject>) private const cCap = 16; { The circular list of cached items } private protected public constructor Create; end; { TSymbolBufferList } constructor TSymbolBufferList.Create; var i: integer; begin inherited Create; { Compiles and runs OK } // inherited; // Compiles but Fails at Runtime, apparently only calls // TObject.Create but does not call AfterConstruction ? } for i := 0 to cCap - 1 do Add(nil); // <<<<<<<<------------- ERROR HERE end; var vSymbolBufferList: TSymbolBufferList;begin vSymbolBufferList := TSymbolBufferList.Create; vSymbolBufferList.Free; ReadLn;end.I'am of the opinion that line 31 inherited; should behave the same as line 30 inherited Create;
Some doc saying otherwise ?
jamie:
Try defining your class as "class" only. No tobject.
Thaddy:
No, that is wrong. Do not specialize to the root class (TObject) but to the class you actually mean to use: TfpgObectList has already the postfix modifier<T:class>.
What that means is that the list will only accept class instances (class) of type T.
What is so difficult to understand?
(btw: you are out of form, lately, Jamie :-X :-[)
Thaddy:
@BrunoK
You can not add nil, because nil is untyped.
WHAT are you trying to achieve?
Maybe casting nil to Tobject will be accepted but is bad code: Add(TSomeObject(nil));
But I would reconsider my design first. Because class instances are pointers and you 'add' duplicates.(nil is a pointer type)
I don't think that is what you want? Describe it better.
Then I can explain it.
Are you maybe trying to create storage for uninitialized future instances? Then you are clueless.(and you are not a beginner)
Better description - not the code, but its purpose - leads to better help.
jamie:
The "Inherited" call without a specified Create looks like it expects the inherited create to be the same, with no parameters.
Since there is no Constructor to override of the same parameters, I guess it's getting confused.
I bet it also gets confused in non-generic code, too.
Navigation
[0] Message Index
[#] Next page
[*] Previous page