executing a child method when pointed to by a base class pointer

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Is there a way of defining a method in a base class such that derived
classes will call their own version, EVEN if the derived instance is
referred to by a pointer to the base class? Note that the base class method
is not to be abstract, and will be called if the instance was created as a
'generic' base class instance.

It's sort of like I want the method to be abstract to children, but concrete
at the base level. That way I can refer to an array of pointers to base
class elements that each will call its child's version of the method when
asked to execute this method (without being told (i.e., cast to) it's of
'child class' type).

Is this possible? I could create an abstract class that has this method as
abstract and derive both 'base class' and 'child class' from it, in which
case a pointer to the abstract class that holds an instance of the base or
child class will execute the approriate method. But then the 'child class'
is no longer a child of the 'base class'. I'd rather do it like I described
above...

Thanks in avance for responses! : )

[==P==]


.



Relevant Pages

  • Re: c++ : a baseclass, derived classes, a list of baseclass pointers, a problem.
    ... > I overwrote the virtual function func1() in each derived class. ... > How can I execute the right function? ... > I use a pointer to the baseclass to loop trough ... You could cast to the base class and investigate the vtable pointer, ...
    (comp.programming)
  • Re: Delegate problem
    ... A child class cannot raise an event designed in the ancestor class. ... I want to execute a delegate in tha base class from a derived ...
    (microsoft.public.dotnet.framework)
  • Re: Inheritance
    ... It seems that if I have code (that references an object) in the OnLoad event ... of the base class this is when I get problems with the designer for the ... > can add the extra buttons and code specific to the child class. ... > something along these lines in the design view window, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: executing a child method when pointed to by a base class pointer
    ... ref childClass: public baseClass ... virtual void func() override ... >> Is there a way of defining a method in a base class such that derived ... >> to) it's of 'child class' type). ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Overides
    ... If your class inherits from a Base Class it can use it's methods. ... has defined for use in your child class then you can sign it with overrides. ... Overrides Function CalculateSomething(val as Int32) as int32 ...
    (microsoft.public.dotnet.framework.aspnet)