visual c++ - SOLVED: Call derived class method from within base class? -
I am trying to call a non-virtualized base class method, which is a derived method of the virtual class of the base class Overrides one of them. For my life, I can not get it to work. See the code below:
Public Reef class base class {Virtual Zero overridden mode () {} void run () { // Do some initial work here. // Call the version of the derivative class of the orriden method to do the final work: override mode (); }}; Public Reff Class DeriveClass: Public BaseClass {Virtual Zero Overridden Mode (override) // // Do some final work here. }}; Int main () {DerivedClass ^ DC = GCN DerivedClass (); DC & gt; Run (); }
Using the above code, the override mode of the base class is called the place of the override mode of the derived class, which is what I do not want. I was under the impression that, while using polymorphism, call the base class method which is overridden, instead the overwritten method of the derived class should be called.
I have also tried to use the abstract:
Public Reef Class Base Class Summary {Virtual Zero Override Mode (abstract); Run zero () {// some initial work here // Call the version of the derivative class of the orriden method to do the final work: override mode (); }}; Public Referee DeriveClass: Public BaseClass {void OverriddenMethod ()} {// Some final work is here}}; Int main () {DerivedClass ^ DC = GCN DerivedClass (); DC & gt; Run (); }
but it returns a C3278 compiler error (the direct call of the interface method will fail on the 'Interface Method' runtime) when OverriddenMethod is called within the BaseClass :: Run () definition is. / P>
What am I doing wrong here? thanks to all!
Edit: Through the answers given below, I was able to find the problem. Here's the solution:
Public Reef Class Base Class Summary {Virtual Zero Base Class :: Override Mode (abstract); Zero Base Class :: Run () {// Here are some basic tasks. // Call the version of derived class of override mode to do the final job: BaseClass :: OverriddenMethod (); // DO NOT DO IT !!!!!!!! OverriddenMethod (); // This is the BEST !!!!! }}; This issue was here because I was using the definitions and CPP file for the implementation of my base class. In the CPP file, I usually use the classname name: username signaling for members of any class name. However, by doing so, the virtual system of the base class is forced to call it as a derivative method in an ineffective method. This is the reason why, while using abstract keyword, I was getting the C3278 error calling the override mode () inside the run () definition of the base class. The reason for this is that I was calling base class :: override mode (), which does not say anything from an abstract definition.
I really hate the very stupid, extremely easy and unseen problems.
Your first example of at least code will not be compiled because private access Have control.
I tried my code for simplicity with some modifications in names and I found the necessary result
#include "stdafx.h" using namespace system; Public Ref Class A {Public: Zero F () {g (); } Virtual Zero (g) {console :: lineline ("a :: g ()"); }}; Public Reef Class B: Public A {Public: Virtual Zero (override) {Console :: Linline ("B :: G ()"); }}; Int main (array
is output
b :: g ()
or classes can be defined < / P>
Public ref class A {public: zero F () {g (); } Secure: Virtual Zero () {console :: lineline ("a :: g ()"); }}; Public Reef Class B: Public A {Secure: Virtual Zero (override) • Console :: Linline ("B :: G ()"); }}; The result is that the derivative class is called the virtual work.
Comments
Post a Comment