07.10.2010 - 16:36
Passing child reference to variable mother reference not possible ?
type
TBase = class // mother
public
constructor Create; virtual;
end;
type
TBaseClass = class of Tbase;
var
mClassType : TBaseClass;
procedure TestCreate( out ParaObject : TBase );
begin
ParaObject := mClassType.Create;
end;
// conceptual user code:
type
TChild = class(TBase) // child
public
constructor Create; override;
end;
procedure Test;
var
vObject : TChild;
begin
// trying to pass child reference to variable mother reference
TestCreate( vObject );
end;
TestCreate does not compile in Delphi 2007 with error message:
"
[Pascal Error] [Unit].pas(95): E2033 Types of actual and formal var
parameters must be identical"
The strange thing i: if TestCreate would be changed to:
procedure TestCreate( out ParaObject : TObject );
then I am pretty sure that it would compile fine...
So the question is:
"Why does Delphi allow (var Sender : Tobject) parameters but not (var Sender
: TSomethingElse) ???"
Is it a shortcoming in the Delphi 2007 compiler/language ?!?
It seems so...
Bye,
Skybuck.
07.10.2010 - 16:45
Hmm I was wrong about it being allowed for (var Sender : TObject)
parameters... it is only allowed for (Sender : TObject) parameters...
This is unfortunate because I was hoping to get rid of these nasty and
boring typecasts which are now needed:
program TestProgram;
{$APPTYPE CONSOLE}
{
Test passing child reference to variable mother reference
version 0.01 created on 7 october 2010 by Skybuck Flying
}
uses
SysUtils;
type
TMother = class
public
mData : string;
end;
TChild = class(TMother)
public
mData2 : string;
end;
procedure TestCreate1( var Para : TObject );
begin
Para := TChild.Create;
end;
procedure TestCreate2( var Para : TMother );
begin
Para := TChild.Create;
end;
procedure Main;
var
vChild1 : TChild;
vChild2 : TChild;
begin
// TestCreate1( vChild1 ); // not allowed
// TestCreate2( vChild2 ); // not allowed
// these typecasts bore me to death.
TestCreate1( TObject(vChild1) );
TestCreate2( TMother(vChild2) );
if vChild1 is TChild then
begin
writeln('ok1');
end;
if vChild2 is TChild then
begin
writeln('ok2');
end;
end;
begin
try
Main;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
ReadLn;
end.
Bye,
Skybuck.
parameters... it is only allowed for (Sender : TObject) parameters...
This is unfortunate because I was hoping to get rid of these nasty and
boring typecasts which are now needed:
program TestProgram;
{$APPTYPE CONSOLE}
{
Test passing child reference to variable mother reference
version 0.01 created on 7 october 2010 by Skybuck Flying
}
uses
SysUtils;
type
TMother = class
public
mData : string;
end;
TChild = class(TMother)
public
mData2 : string;
end;
procedure TestCreate1( var Para : TObject );
begin
Para := TChild.Create;
end;
procedure TestCreate2( var Para : TMother );
begin
Para := TChild.Create;
end;
procedure Main;
var
vChild1 : TChild;
vChild2 : TChild;
begin
// TestCreate1( vChild1 ); // not allowed
// TestCreate2( vChild2 ); // not allowed
// these typecasts bore me to death.
TestCreate1( TObject(vChild1) );
TestCreate2( TMother(vChild2) );
if vChild1 is TChild then
begin
writeln('ok1');
end;
if vChild2 is TChild then
begin
writeln('ok2');
end;
end;
begin
try
Main;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
ReadLn;
end.
Bye,
Skybuck.
Ähnliche Themen
Re: question on passing variable to function by reference
04.11.2010 - 10:02 - Posts: 1
Re: question on passing variable to function by reference
06.11.2010 - 10:59 - Posts: 1
Passing form reference as variable to function
30.03.2010 - 23:39 - Posts: 5
question on passing variable to function by reference
01.11.2010 - 11:01 - Posts: 3
passing by reference
27.07.2009 - 20:27 - Posts: 11
Passing a value by reference
13.01.2012 - 21:03 - Posts: 4
passing by reference
16.12.2009 - 20:21 - Posts: 17
More
Search
