Jump to content
wqmeng

Why TFooBaseClass = class of TFooBase in parameter can not compile, but TClass could?

Recommended Posts

I have a function which need pass a class referrence in as a parameter.

So I have
 

type

TFooBase = class;

TFooBaseClass = class of TFooBase;

TFooA = class(TFooBase)

end;

TFooB = class(TFooBase)

end;


//Then in another class

type

TBar = class

public

  function FindFooByClass(AFooClass: TFooBaseClass): TFooBase;

end;

implement

function TBar.FindFooByClass(AFooClass: TFooBaseClass): TFooBase;

 

 

Then at the line function TBar.FindFooByClass(AFooClass: TFooBaseClass): TFooBase;

Delphi report
[dcc32 Error] xxx: E2037 Declaration of 'FindFooByClass' differs from previous declaration,  But as you see, the Declaration is same.

 

And if I change TFooBaseClass to be TClass in both the declaration and the implement as

function FindFooByClass(AFooClass: TClass): TFooBase;

function TBar.FindFooByClass(AFooClass: TClass): TFooBase;

Then the source will complile. 

Just want to know that why TFooBaseClass not work?

 

 

Edited by wqmeng

Share this post


Link to post
3 minutes ago, wqmeng said:

So I have

You do not have that. Please provide a complete example, that has the correct spelling of implementation, and the declaration for TFooBase, e.g.

unit Unit2;

interface

type

TFooBase = class;

TFooBaseClass = class of TFooBase;

TFooBase = class

end;


TFooA = class(TFooBase)

end;

TFooB = class(TFooBase)

end;


//Then in another class

type

TBar = class

public

  function FindFooByClass(AFooClass: TFooBaseClass): TFooBase;

end;

implementation

function TBar.FindFooByClass(AFooClass: TFooBaseClass): TFooBase;
begin

end;

end.

Which incidentally, compiles. Also, please just ask your question in one group.

Share this post


Link to post

Hello Dave,

 

Thank you, I just create an empty project and add the code above,  It could compiles.

 

But in my work project, it use the similar approach, get the error as

[dcc32 Error] xxx.pas(517): E2037 Declaration of 'FindIndicatorByClass' differs from previous declaration. 

 

Not sure why.

Share this post


Link to post

Sorry,

 

Find the problem that the 

TFooBaseClass = class of TFooBase;

 

Was defined in two units, after delete one, the problem get fixed.

 

Thanks.

Share this post


Link to post
22 hours ago, wqmeng said:

Sorry,

 

Find the problem that the 


TFooBaseClass = class of TFooBase;

 

Was defined in two units, after delete one, the problem get fixed.

 

Thanks.

The best piece of advice you can take from here is that you need to learn how to ask questions effectively. Because if you ask like this then nobody can help you. And it's just a waste of your time. 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×