From: James Turner Date: Wed, 24 Aug 2011 09:30:02 +0000 (-0700) Subject: Copy constructor and assignment operator for revised IPAddress X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b7654c181dc8d52875365b170bc3092e8e51d68f;p=simgear.git Copy constructor and assignment operator for revised IPAddress --- diff --git a/simgear/io/raw_socket.cxx b/simgear/io/raw_socket.cxx index 8bcae985..7fe2c688 100644 --- a/simgear/io/raw_socket.cxx +++ b/simgear/io/raw_socket.cxx @@ -67,6 +67,30 @@ IPAddress::IPAddress ( const char* host, int port ) set ( host, port ) ; } +IPAddress::IPAddress( const IPAddress& other ) : + addr(NULL) +{ + if (other.addr) { + addr = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in)); + memcpy(addr, other.addr, sizeof(struct sockaddr_in)); + } +} + +const IPAddress& IPAddress::operator=(const IPAddress& other) +{ + if (addr) { + free(addr); + addr = NULL; + } + + if (other.addr) { + addr = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in)); + memcpy(addr, other.addr, sizeof(struct sockaddr_in)); + } + + return *this; +} + void IPAddress::set ( const char* host, int port ) { addr = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in)); diff --git a/simgear/io/raw_socket.hxx b/simgear/io/raw_socket.hxx index 6ae529f2..2b5031f3 100644 --- a/simgear/io/raw_socket.hxx +++ b/simgear/io/raw_socket.hxx @@ -46,6 +46,9 @@ public: IPAddress ( const char* host, int port ) ; ~IPAddress(); + IPAddress( const IPAddress& other ); + const IPAddress& operator=(const IPAddress& other); + void set ( const char* host, int port ) ; const char* getHost () const ; unsigned int getPort() const ;