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

Vinod K Chandran kcvinu82 at gmail.com
Sat May 23 08:32:34 UTC 2020


On Friday, 22 May 2020 at 22:44:17 UTC, H. S. Teoh wrote:
> 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

Yeah, I understand that. And i just changed my code. Thanks for 
the guidance. :)


More information about the Digitalmars-d-learn mailing list