How to use base class & child class as parameter in one function ?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri May 22 22:44:17 UTC 2020


On Fri, May 22, 2020 at 09:39:16PM +0000, Vinod K Chandran via Digitalmars-d-learn wrote:
[...]
> So in the same manner, i want
> void function(Base) = fnPtr wiil work with
> void function(Child)

You cannot, because that's type unsafe:

	class Base {}
	class Derived : Base {
		void derivedFunc() {}
	}
	class Another : Base {}

	void derivedFunc(Derived d) { d.derivedFunc(); }

	void function(Base) funPtr;
	funPtr = derivedFunc; // suppose this was allowed

	Base obj = new Another;
	funPtr(obj); // crash: obj does not have derivedFunc()


T

-- 
Век живи - век учись. А дураком помрёшь.


More information about the Digitalmars-d-learn mailing list