Copy constructor and assignment operator for revised IPAddress

This commit is contained in:
James Turner
2011-08-24 02:30:02 -07:00
parent 50e226a146
commit b7654c181d
2 changed files with 27 additions and 0 deletions

View File

@@ -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));

View File

@@ -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 ;