Re: [fpc-pascal] Problem with virtual constructors

2011-06-01 Thread cobines
2011/6/1 Jorge Aldo G. de F. Junior :
> 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;
>

TTreeNode is a different type than TTreeNodeWithProperties.

I think you should override the constructor with base type as param:

   Constructor Create(Const aOwner :  TTreeNode); Override;

At runtime check with:

if not (aOwner is TTreeNodeWithProperties) then
  raise exception
inherited Create(aOwner);
...

If you define a different constructor with the derived type as param :

   Constructor Create(Const aOwner :  TTreeNodeWithProperties); Overload;

then if you're passing TTreeNode objects to   TTreeNodeWithProperties
constructor it will only call the base constructor.

--
cobines
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Problem with virtual constructors

2011-06-01 Thread Jorge Aldo G. de F. Junior
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.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal