View Antoni Milton's profile on LinkedIn

Wednesday, August 24, 2011

Call the destructor more than one time

we can call the destructor as like function, but we can't call the constructor like this.

class Shape
{
   public:
       Shape(){ cout<<" Shape"<
       virtual ~Shape(){ cout<<" ~  Shape"<
};
class Circle : public Shape
{
   public:
      Circle() { cout<<"Circle"<< endl; }
      ~Circle(){ cout<<" ~ Circle" << endl; }
};

int main()
{

   Circle *ptr = new Circle();
   ptr->~Circle();
   ptr->~Circle();

   return 1;
}

output:-

# ./a.out
 Shape
Circle
 ~ Circle
 ~  Shape
 ~  Shape

while calling the destructor function first time , its calling the base call destructor also ( if its virtual ) . but after that not calling the base class destructor.

Tuesday, August 9, 2011

How to resolve the IPv6 Address from domain name in C++ ?

In general, we are using gethostbyname or gethostbyname_r for resolving IPAddress from domain name. These system calls are not supporting IPv6 format.

For resolve IPv6Address from domain name, we need to use gethostbyname2_r function.

struct in6_addr aadrv6;
struct hostent *host;
char* pszHostName = “localhost6.localdomain6”;
char strIPAddr[50];
char buffer[2048];
int iError = 0,hrr;

iError = gethostbyname2_r( pszHostName, AF_INET6, &hostbuf,buffer, 2048,&host,&hrr );

if( iError == 0 && host != NULL )
{
    memcpy( &aadrv6 , host->h_addr_list[0], sizeof( in6_addr ) );
   if( inet_ntop ( AF_INET6, &aadrv6, strIPAddr, INET6_ADDRSTRLEN ) != NULL )
   {
        printf("IPv6Address: %s\n", strIPAddr);
    }
}

output:
#./a.out
IPv6Address: ::1