Copy constructor and assignment operator for revised IPAddress
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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 ;
|
||||
|
||||
Reference in New Issue
Block a user