Question

I have an example class with two class functions Foo. One takes an argument, the other doesn't:

TContoso = class
   class function Foo: IUnknown; overload;
   class function Foo(bar: IUnknown): IUnknown; overload;
end;

And i can call my static function without incident:

unk := TContoso.Foo;

But if the methods were named what i wanted to call them in the first place:

TContoso = class
   class function Create: IUnknown; overload;
   class function Create(bar: IUnknown): IUnknown; overload;
end;

Then the same class method call:

unk := TContoso.Create;

fails to compile:

Ambiguous overload call to 'Create'

Why is the non-ambiguous call ambigious? I hope it's not an arbitrary thing.

Why would you do that?

Not that it matters to the question, but if we ignore the question and focus on the situation i'm in, some would recognize the pattern:

CoXmlWriter = class
   class function Create: IXmlWriter; overload;
   class function Create(const stream: ISequentialStream): IXmlWriter; overload;
end;
Était-ce utile?

La solution

I believe that the ambiguity is with the constructor declared in TObject that has identical parameters.

However, in modern versions of Delphi the code compiles. And I think that is right because the TObject constructor is not marked with overload. So this feels like a Delphi 5 compiler bug.

Autres conseils

Try this:

Contoso = class
   class function Create: IUnknown; overload; reintroduce;
   class function Create(bar: IUnknown): IUnknown; overload;
end;

(or perhaps switch around overload; and reintroduce; modifiers)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top