[fpc-pascal] Problem with virtual constructors

Jorge Aldo G. de F. Junior jagfj80 at gmail.com
Wed Jun 1 18:26:59 CEST 2011


I am having problems with virtual methods.

I have two classes, the second one is a descendent of the first :

1st:

	TTreeNode = Class(TObject)
	Public
		Constructor Create(Const aOwner : TTreeNode); Virtual;

2nd :
	TTreeNodeWithProperties = Class(TTreeNodeWithName)
	Public
		Constructor Create(Const aOwner : TTreeNodeWithProperties); Override;

TTreeNodeWithName is the ancestor of TTreeNodeWithProperties, and
TTreeNode is the ancestor of TTreeNodeWithName.

TTreeNode -> TTreeNodeWithName -> TTreeNodeWithProperties

TTreeNodeWithName use the old constructor from the base class (IE.: It
doesnt define a new constructor), so it works ok.

TTreeNodeWithProperties needs to hook the constructor/destructor to
build some aditional encapsulated objects,
so i need to Override the virtual constructor.

As you can see i am using virtual constructors.

The problem is :

If i use Override it causes this output from the compiler :

{
Free Pascal Compiler version 2.4.4 [2011/04/22] for i386
Copyright (c) 1993-2010 by Florian Klaempfl
Target OS: Linux for i386
Compiling htmlnodes.pas
Compiling tree.pas
Compiling namevalue.pas
tree.pas(89,15) Error: There is no method in an ancestor class to be
overridden: "constructor TTreeNodeWithProperties.Create(const
TTreeNodeWithProperties);"
tree.pas(125,1) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
}

If i use Reintroduce the compiler doesnt complain,
but when the class is instantiated from a class factory,
it builds the wrong class (the constructor called is of the base class
not of the derived one).
IE.: The aditional objects i need in the derived class are not
instantiated, giving a big access violation exception during runtime.

If i leave without override or reintrodce, the compiler complains :

{
Free Pascal Compiler version 2.4.4 [2011/04/22] for i386
Copyright (c) 1993-2010 by Florian Klaempfl
Target OS: Linux for i386
Compiling htmlnodes.pas
Compiling tree.pas
Compiling namevalue.pas
tree.pas(89,15) Warning: An inherited method is hidden by "constructor
TTreeNodeWithProperties.Create(const TTreeNodeWithProperties);"
htmlnodes.pas(34,15) Error: There is no method in an ancestor class to
be overridden: "constructor
THTMLNode.Create(TTreeNodeWithProperties);"
htmlnodes.pas(80,1) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
}

I think i am doing something wrong...
I'm left without options...
Pls Help.



More information about the fpc-pascal mailing list