c++ - Refactoring a leaf class to a base class, and keeping it also a interface implementation -


I am trying to reactivate a working code. I code basically receives the interface class in a functional implementation , And I want to use this implementation outside of the original project as an original class.

However, I do not want to make a fork, and I want to use the original project to be able to carry out its implementation, and use me the problem is that the hierarchy structure is very different and I'm not sure That it will work. I can not even use the basic base class in my project, because in fact it is quite confused in the project (it is included in very large sections) and I only have to take care of a subdomain of the problems of the original project.

I wrote this code to test how to implement it, and it is working, I'm not sure I like it:

  #include & lt; Iostream & gt; // The original code is: // IBase - & gt; Derived1 // I need to redistribute Derive2 to be both of the immutable class / and programmer interface class // Derived2 - & gt; MyClass + IBase // MyClass Class IBase {Public: Virtual Zero printMsg () = 0; }; //////////////////////////////////////////////// / Class Derived1: Public IBase {Public: Virtual Zero printMsg () {std :: cout & lt; & Lt; "Hello 1 Taken from" & lt; & Lt; Std :: endl; }}; //////////////////////////////////////////////// Class MyLess {Public: Virtual Zero Print Message () {std :: cout & lt; & Lt; "Hello from Michael" & lt; & Lt; Std :: endl; }}; Class Derived2: Public IBase, Public MyClass {Virtual Zero printMsg () {MyClass :: printMsg (); }}; Class Derive3: Public MyClass, Public IBase {Virtual Zero printMsg () {MyClass :: printMsg (); }}; Int main () {IBase * o1 = New Derived1 (); IBase * o2 = new Derived2 (); IBase * o3 = new Derived3 (); MyClass * o4 = New MyClass (); O1-> PrintMsg (); O2-> PrintMsg (); O3-> PrintMsg (); O4- & gt; PrintMsg (); Return 0; }  

The product is working as expected (tested using GCC and Clag, 2 different C ++ implementations, so I think I am safe here ):

  [Hello from MyClass [Elkko @ Pinky ~ / src / googlecode / qtedit4 / tools / qtsourceview / my_slow) Hello from MyClass Qute / Test] ./test1.clang Hello from 1 by Michael. MyClass Hello MyClass  



Question

My original code was:

  Class Derived3: Public MyClass, Public IBase {Virtual Zero IBase :: printMsg () {MyClass :: printMsg (); }};  

I am what I want to express, but it does not compile. I have to admit that I do not fully understand why this code works, as I expect the new method Derived3 :: printMsg () MyClass :: Implementation of printMsg () and no IBase :: printMsg () (It is also difficult to do what I want to do ).

How does the compiler select a method when two "sister squares" have the same virtual function name?

If someone has a better way of implementing this, then I would also like to know :)

The answer is, compiler overdrides both functions, as can be seen by this sample:

  #include  

Output is:

  via calling C :: AC :: AA :: AB :: A calling C :: A by BC :: AA :: A :: A  

If you want to override one and not the other, then you need to rename it. Not only will you do what you want but it will also be clear. / P>


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -