Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Apr 23, 2004

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);
}
Saurabh Ramya
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date