Hi
When I try to make new class, which inherited from another class I found, as I understand, a mistake in working a compiled code.
Code Example:
char s[1024]; class Parent{ public: Parent(){ vMethod(); }; virtual void vMethod(){ sprintf(s,"Parent virtual method"); }; }; class Child: Parent{ public: Child(){ vMethod(); }; virtual void vMethod(){ sprintf(s,"Child virtual method"); }; }; int main(void){ Child* c = new Child(); ...
So, what happends. At this code the methos and constructor calling should to be the next: 1. Calling the Parent constructor, which call overrided vMethod of Child class 2. Calling the Child constructor, which again call overrided vMethod of Child class.
But the Parent construcor calls Parent vMethod and the Child constructor calls Child vMethod. So clasic overriding is works not correct.
The clasic construction on overrided method shoilt to looks like: override void vMethod(){} or virtual void vMethod() override{} but none of this code construction dosn't work.
So it's a error in compilator or I shoult to use another construction for correct overriding virtual class methods.
Thanks.
I think that reason of overriding virtual methods is to call a new emplementation of method even if it calls from parent class method.
Your thinking is correct. You just cannot call overridden virtual methods from a constructor / destructor. ( even using this->constructor(); )
Of cause correct. May be its not correct from side of standard c++11 I show one example on c# one of most robust oop language. May be its not correct to compare with c++, but its show an essence of the idea.
class Parent { public Parent() { vMethod(); sMethod(); } public virtual void vMethod() { Debug.WriteLine("vParent"); } public void sMethod() { Debug.WriteLine("sParent"); } } class Child : Parent { public Child() { vMethod(); sMethod(); } public override void vMethod() { Debug.WriteLine("vChild"); } public new void sMethod() { Debug.WriteLine("sChild"); } } ... static void Main(string[] args) { Child c = new Child(); ...
The result of program working: vChild sParent vChild sChild
So as we can see, when start constructor of Child class its call Parrent constructor, which calls vMethod() and sMethod(). vMethod() is overriden, so it print "vChild", sMethod is simple class method, so it print "sParent". After Parent constructor calls child constructor code. It again calls vMethod() and sMethod(). vMethod() is overriden, so it again print "vChild". sMethod is declarated as new, so when it calls from Child class, calls new version of sMethod. So it print "sChild".
In this case operators "override" and "virtual" has the meaning. Now its like a "new" operator.
Now in ARM compiler "virtual" operator it like "new" operator in example above, but from the poiny of oop "override" is redefinition of method address in previous position of vertual method table.
When some years ago read how it works, I read that for virtual methods plased table in memory with methods addresses. When you override method in child class in child class table on plase of method putted new address and the old address putten at new plase at the end of virtual method table. So you have a posibility to call old method from parent (for example above
public override void vMethod() { base.vMethod(); Debug.WriteLine("vChild"); }
)