Monday, 11 July 2016

Priority of implicit conversion of user defined type and system defined type

 
Guess the output?

#include<iostream>
#include<string>
class ArrayNotFound
{
public:
       bool learnSomethingNew;
      
       ArrayNotFound(const char *p_char_pointer)
       {
             std::cout<<"ArrayNotFound constructor"<<std::endl;   
       }
};

void PostInArrayNotFound(const void *)
{         
std::cout<<"void pointer parameter"<<std::endl;
}

void PostInArrayNotFound(ArrayNotFound p_obj)
{
std::cout<<"ArrayNotFound class parameter"<<std::endl;     
}

int main()
{
 char *a="HAI";
PostInArrayNotFound("DD");   
return 0;
}

Output:


void pointer parameter

EXPLANATION:


Here PostInArrayNotFound is an overloaded function with parameter as follows

1.       Void pointer
2.       ArrayNotFound object(Since parameter of ArrayNotFound  constructor is char pointer call to PostInArrayNotFound("DD") will inturn call an ArrayNotFound   constructor when “void PostInArrayNotFound(const void *)” is not present in the program)
Here compiler has rights to call any one of the function. But since compiler can’t behave as it likes, standard said to follow the following rule.

System defined implicit conversion(void pointer) will take the high priority when compared to the user defined implicit conversion.