Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread Virtual methods / constructors
Sat, Oct 10 2015 7:26 AMPermanent Link

Christian Kaufmann

Not sure, if this is as designed or an error in the compiler:

type
 TBSMenuItem = class(TLabelControl)
 public
   constructor Create(AOwner: TComponent; const ACaption: String);
 end;


Now both of the following statements compile:

 Result := TBSMenuItem.Create(Owner);

 Result := TBSMenuItem.Create(Owner, ACaption);

For my understanding, the first one should give a compiler error because the base declaration is
hidden by a new one.

Then when I do this:

 TBSMenuItemGroup = class(TBSMenuItem)
 public
   constructor Create(AOwner: TComponent; const ACaption: String); //override;
 end;

The compiler complains, because the constructor is not declared as "override". But it's not enough
todo that. I have to declare the TBSMenuItem.Create as virtual as well.

cu Christian
Sat, Oct 10 2015 11:09 AMPermanent Link

Raul

Team Elevate Team Elevate

"Christian Kaufmann" wrote:
<<
type
 TBSMenuItem = class(TLabelControl)
 public
   constructor Create(AOwner: TComponent; const ACaption: String);
 end;


Now both of the following statements compile:

 Result := TBSMenuItem.Create(Owner);

 Result := TBSMenuItem.Create(Owner, ACaption);

For my understanding, the first one should give a compiler error because the base declaration is
hidden by a new one.
>>

How is it overridden? Class can have multiple constructors and you can only override ones with same param list.
You've simply added a new constructor with an additional parameter.

If you need to override you could do something along the lines of :

 TBSMenuItem = class(TLabelControl)
 public
   constructor Create(AOwner: TComponent); override;
   constructor Create(AOwner: TComponent; const ACaption: String);
 end;

and then your Create(owner) implementation could simply call Create(owner,'') to keep your code in a single constructor (and of course call inherited there as well).

Raul
Sat, Oct 10 2015 12:31 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Christian,

<< For my understanding, the first one should give a compiler error because the base declaration is hidden by a new one. >>

EWB does not require the use of the overload keyword for overloaded methods/properties, therefore any method with a different signature is automatically considered an overload.

Tim Young
Elevate Software
www.elevatesoft.com
Sat, Oct 10 2015 3:01 PMPermanent Link

Christian Kaufmann

Tim Young [Elevate Software] wrote:

> EWB does not require the use of the overload keyword for overloaded methods/properties, therefore
> any method with a different signature is automatically considered an overload.

Ok. And all methods with same signature in a subclass have to be virtual/overload? The following
code doesn't compile without virtual/override:



unit Unit2;

interface

uses WebCore, WebLabels;

type
 TBSMenuItem = class(TLabelControl)
 public
   constructor Create(AOwner: TComponent; const ACaption: String); virtual;
 end;

 TBSMenuItemGroup = class(TBSMenuItem)
 public
   constructor Create(AOwner: TComponent; const ACaption: String); override;
 end;


implementation

constructor TBSMenuItem.Create(AOwner: TComponent; const ACaption: String);
begin
 inherited Create(AOwner);
 Caption := ACaption;
end;

constructor TBSMenuItemGroup.Create(AOwner: TComponent; const ACaption: String);
begin
 inherited Create(AOwner, ACaption);
 // some more is done here
end;

end.
Mon, Oct 12 2015 11:30 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Christian,

<< Ok. And all methods with same signature in a subclass have to be virtual/overload? >>

Yes.

Tim Young
Elevate Software
www.elevatesoft.com
Image