[Ns-developers] C++ virtual method overloading andinheritance = disaster?
craigdo@ee.washington.edu
craigdo at ee.washington.edu
Wed Jul 9 17:38:51 PDT 2008
Without comment ...
class SocketFactory : public Object
{
public:
static TypeId GetTypeId (void);
SocketFactory ();
/**
* \return smart pointer to Socket
*
* Base class method for creating socket instances.
*/
virtual Ptr<Socket> CreateSocket (void) = 0;
};
class TcpSocketFactory : public SocketFactory
{
public:
static TypeId GetTypeId (void);
virtual Ptr<Socket> CreateSocket (void) = 0;
};
> -----Original Message-----
> From: ns-developers-bounces at ISI.EDU
> [mailto:ns-developers-bounces at ISI.EDU] On Behalf Of Mathieu Lacage
> Sent: Tuesday, July 08, 2008 7:40 PM
> To: Gustavo Carneiro
> Cc: ns-developers
> Subject: Re: [Ns-developers] C++ virtual method overloading
> andinheritance = disaster?
>
>
> 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