views:

79

answers:

2

Hi. I'm an intermediate C++ user and I encountered the following situation. The class definition shown below compiles fine with a g++ compiler. But I cannot put my finger on what exactly the whole syntax means.
My guess is that the function operator int() returns an int type.

Moreover, I cannot figure out how to use the overloaded operator () in main()

class A
{
   public:
     A(int n) { _num = n; }  //constructor 

     operator int();

   private:
     int _num;
};

A::operator int()  // Is this equivalent to "int A::operator()" ??
{
  return _num;
}

int main()
{
  int x = 10;
  A objA(x);  //creating & initializing

  // how to use operator() ?
  // int ret = objA();   // compiler error when uncommented

  return 0;
}

Any help will be appreciated.
Thanks.

Vthulhu
~ Hack The Planet ~

+5  A: 

operator int() is a conversion function that declares a user-defined conversion from A to int so that you can write code like

A a;
int x = a; // invokes operator int()

This is different from int operator()(), which declares a function-call operator that takes no arguments and returns an int. The function-call operator allows you to write code like

A a;
int x = a(); // invokes operator()()

Which one you want to use depends entirely on the behavior that you want to get. Note that conversion operators (e.g., operator int()) can get invoked at unexpected times and can cause pernicious errors.

James McNellis
Thanks! This is cool stuff!
vthulhu
@vthulu:U seem to be new to community so just for your information, if you are satisfied with the above answer, you would want to select the tick mark which appears just beside this answer which essentially means that you accept the answer
hype
"Note that conversion operators (e.g., operator int()) can get invoked at unexpected times and can cause pernicious errors." I'd word this somewhat stronger: __Stay away from them!__
sbi
@hype Done. Thanks!I've just been a reader of StackOverflow for quite some time now but I never really contributed. As they say - "Necessity ..."
vthulhu
@sbi: Yeah, that's good, bold advice. :-P Every time I've implemented one it's been a hack. At least in C++0x, with explicit conversion operators, the perniciousness will be significantly less.
James McNellis
@James: The same here. Everyone I've written in the last decade (well, in the first half of it anyway; I've learned my lesson since), I had to remove it later (sometimes painfully) because it would be called at unexpected times and cause problems. Oh, and I wish someone with half a decade of experience in C++1x would come out with a good book of Dos and Donts for it.
sbi
@sbi: Well, hopefully we'll see [some new Guru of the Week "contests"](http://meta.stackoverflow.com/questions/62697/stack-overflow-and-gotw-what-happened-with-this) in the near future here on Stack Overflow :-)
James McNellis
@James: Oh, I hadn't known that! But unless I'm missing something, the odds for this to happen seem not very good.
sbi
A: 

you can use this one

#include <iostream>
using namespace std;
class A{
public:
    A(int n)  { _num=n;}
    operator int();

private:
    int _num;

};
A::operator int(){

    return _num;

}
int main(){

    A  a(10);
    cout<<a.operator int()<<endl;
    return 0;

}