devxlogo

Use User-defined Where Built-in Is Expected

Use User-defined Where Built-in Is Expected

Say you’ve got a function that takes some built-in kind of parameter. You need to make that function work?even for an object (which has a data member of that built-in type type)?all without changing the syntax of your function call statement. This code can do it for you:

class INT{public:   INT( int t): y(t)   {}  operator int ()  {    return y;  }private: int y;};void func (int y){  cout << y;}int main (){  int t = 9;  func(t);  INT obj_INT(6);  func(obj_INT);}

devx-admin

Share the Post: