[Ns-developers] C++ virtual method overloading and inheritance = disaster?
Mathieu Lacage
mathieu.lacage at sophia.inria.fr
Tue Jul 8 19:39:47 PDT 2008
On Wed, 2008-07-09 at 00:29 +0100, Gustavo Carneiro wrote:
> -----------
> struct Socket
> {
> virtual int Bind () { return -1; }
> virtual int Bind (int address) { return address; }
> };
>
> struct UdpSocket : public Socket
> {
> virtual int Bind () { return 0; }
> };
>
>
> int main(void)
> {
> UdpSocket sock;
> sock.Bind (123);
> }
>
> ---------------
>
> Compiling the above program I get:
>
> gjc at dark-tower:tmp$ g++ test.cc
> test.cc: In function 'int main()':
> test.cc:18: error: no matching function for call to 'UdpSocket::Bind(int)'
> test.cc:11: note: candidates are: virtual int UdpSocket::Bind()
>
> WTF? How come a Bind() method in UdpSocket hides the other Bind() method
> defined in the base Socket class?
>
> Commenting out the Bind() method in UdpSocket makes the program compile.
Yes. Sam pointed out to an explanation of why this happens and why the
fix you describe works.
I have to confess that I do not understand why udp-socket.h redefines
all these pure virtual methods. I suspect that this is due to some
copy/paste action so, I would suggest a patch which cleans up
udp-socket.h as well as tcp-socket.h and removes all pure virtual
methods redefined from socket.h
regards,
Mathieu
More information about the Ns-developers
mailing list