Virtual method call from constructor
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Tue Apr 4 08:21:01 UTC 2023
> Firstly, are all calls virtual in a struct/class in D?
In D structs are always value types without a vtable or inheritance.
Classes are always reference types with a vtable, but some of the
methods may be final and hence not in vtable.
> Second, could you give me some case examples where this problem
occurs? Is the issue if I override refresh in a derived class, and the
base class will accidentally use child.refresh()?
Yes. It'll call the entry in the vtable, not the one declared in the
class if its overridden.
> Third, is there the expectation that you should _never_ call any
internal, private, methods inside a constructor? Or do I just
call/structure it a different way?
No. Do what you need to do. Dscanner is a linter that may be a bit
sensitive just in case you didn't realize that the behavior is doing
something that you probably haven't considered, which could be problematic.
More information about the Digitalmars-d-learn
mailing list