
+2
Planned
Add warning when a class's constructor exists, but the code calls an ancestor's constructor instead
It is quite possible / easy to construct an object by calling a classes' ancestor's constructor, instead of a constructor for the class you want to create. This leaves you with a partially constructed object, not a good thing.
One simple way to demonstrate this:
type
TFoo = class
constructor Create;
end;
TBar = class(TFoo)
protected // !
constructor Create(a : integer);
end;
// In another unit (!)
Bar := TBar.Create; // Calls TFoo.Create because TBar.Create is not visible outside the unit
I'd like a warning something like, "TBar has a constructor defined, but Bar is being constructed with ancestor class TFoo's constructor."
This is not quite as uncommon a circumstance as you might think - as well as the above, which would trip up any C++ programmer who would expect a compile error if it was calling the wrong constructor, there are also possibilities to do this when some but not all constructors are overridden, etc, too. I've seen bugs caused by this several times.
One simple way to demonstrate this:
type
TFoo = class
constructor Create;
end;
TBar = class(TFoo)
protected // !
constructor Create(a : integer);
end;
// In another unit (!)
Bar := TBar.Create; // Calls TFoo.Create because TBar.Create is not visible outside the unit
I'd like a warning something like, "TBar has a constructor defined, but Bar is being constructed with ancestor class TFoo's constructor."
This is not quite as uncommon a circumstance as you might think - as well as the above, which would trip up any C++ programmer who would expect a compile error if it was calling the wrong constructor, there are also possibilities to do this when some but not all constructors are overridden, etc, too. I've seen bugs caused by this several times.
Customer support service by UserEcho
Does using the "reintroduce" key word work: