[[C++]] [[OOP]]

/** @author: http://www.rzr.online.fr
  * How comes typed function overloading break it all ? */
#include <iostream>
using namespace std;
#define CONFLICT // choose one of these not CONFLICT + OVERLOAD
#define OVERLOAD
class Base {
public:
virtual void main() { fvirtual();  } // show polymorphism
virtual void fvirtual( std::ostream &  out = std::cout )
{ out<<"Base::fvirtual"<<endl; } // method to be linked dynamicly
virtual void fvirtual(char* &arg, std::ostream &  out = std::cout )
{ out<<"Base::fvirtual "<< arg <<endl; } // will conflict with derived one
};
class Derived : public Base {
public:
#ifdef CONFLICT
virtual void fvirtual(std::ostream &  out = std::cout )
{ out<<"Derived::fvirtual"<<endl; } // will conflict with base one
#endif
};
int main(int argc, char* argv[[]]) {
Derived cd;  cd.main(); // will illustrate what is polymorphism
//cd.fvirtual( "test" , std::cout);
#ifdef OVERLOAD
Derived* c = new Derived;
/*
  In function `int main(int, char**)':
  error: no matching function for call to `
  Derived::fvirtual(char*&, std::ostream&)'
  error: candidates are: virtual void
  Derived::fvirtual(std::ostream&)
  */
#else
Base* c = new Derived;
/*
  will print
  Base::fvirtual ./cpppolymorphismdefaultparm
  */
#endif
c->main(); // ok it is allways linked
c->fvirtual( argv[[0]] , std::cout); // will cause an error on compilation
return 0;
}
/*
TARGET=cpppolymorphismdefaultparm
CXXFLAGS="-g -Wall -DMAIN_${TARGET}"
make ${TARGET} CXXFLAGS="${CXXFLAGS}"  \
&&  ${TARGET} || gdb ${TARGET}
*/

irc://irc.freenode.net/#c++ helped :

<calvin_> RzR: function lookup and overload resolution are two distinct steps, performed in sequence. in short, the fvirtual(char*... ) becomes hidden in Derived.
<RzR> just because it has the same name ?
<RzR> thats annoying
<PlasmaHH> yes, having the same name is called overloading....
<ryg> hiding rule
<ryg> when you override one overloaded version, you'll have to override them all.
<RzR> the mangled name is different , isnt it ?
<RzR> i thought that parameters where also defining a method
<PlasmaHH> c++ has no concept of mangling names, only the implementations have...
<RzR> ok well it makes sense now
<RzR> so it is tricky to use generic names for functions

Leave comments here

http://rafb.net/paste/results/rnWx7p69.html

virtual.txt · Last modified: 2020/04/20 18:22 (external edit)
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki