2

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.

3
  • Overloaded operator ()? What overloaded operator ()? Your code does not have any overloaded operator (). This is why you can't use it. Commented Sep 3, 2010 at 6:53
  • Yeah. I was very much mistaken The keyword operator took me elsewhere. Commented Sep 3, 2010 at 7:15
  • possible duplicate of What does this C++ syntax mean and why does it work? Commented Sep 3, 2010 at 7:47

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

7 Comments

@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
"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!
@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 ..."
@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: 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.
|
0

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.