3

I came across this code just few minutes back here in Stack Overflow. I was confused about what actually operator int&() { return i; } is doing in the code.

#include <iostream>
#include <conio.h>

using namespace std;

class INT {
public:
            INT(int ii = 0) : i(ii) {}
            operator int&() { return i; }
            void display()
            {
              cout << "value of i is : " << i;       
            }
private:
  int i;
};

int main()
{
  INT l;
  cin >> l;
  l.display();
  getch();
  return 0;
}

I added the display function just to get some insight. I saw that the value that I get from cin >> l; gets assigned to i which is private member of object l. So this is some sort of overloading definitely I guess.

Can you let me know can we take the value of the object of class from directly through cin? Does it work fine?

What if I have two int variables in private part of my class INT?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Invictus
  • 4,028
  • 10
  • 50
  • 80
  • Why not **try** it and see what happens? – Kerrek SB Feb 25 '12 at 14:03
  • @KerrekSB I added a new variable in private part . The `i` was assigned the value that i gave as input from my console and second Variable gives O/P as zero as i initialized it to be so. How can i get both the variables get input from console ? – Invictus Feb 25 '12 at 14:10
  • 1
    Try defining *two* conversion operators; GCC gives me a very clear and helpful error message in that case. – Kerrek SB Feb 25 '12 at 14:13
  • @KerrekSB you mean one of `int` type and other of say `char` type ? – Invictus Feb 25 '12 at 14:15
  • 1
    Please remove that conio.h include unless you want to bring the wrath of MS-DOS gods upon yourself – SomeWittyUsername Aug 31 '13 at 14:38

1 Answers1

13

What is operator int&() { return i; } doing in the code?

It is a Conversion Operator; it takes in an INT object and returns a reference to int type.

One implicit conversion is always allowed whenever the compiler can match function arguments etc.
So, whenever there is a function which takes an int but an INT instance is passed to it, the compiler uses this conversion operator to convert an INT to int, so that appropriate conversion takes place and function can be called.

What is the use of such conversion. Are they helpful in practical code?

As said in the first answer, this is useful whenever your code needs a lot of implicit conversions.
Note that in the absence of such a feature, one would have to provide special member functions which return appropriate types and the user will have to call them explicitly; this is lot of coding overhead, which is avoided by this feature.

Can you let me know can we take the value of the object of class from directly through cin? Does it work fine?    

In order that cin>>l should work, l should be of a data type for which the >> operator is overloaded for type cin(istream)

Note that >> is overloaded for most of the built-in data types, and hence you can directly use, cin>>i, for types where i is int or float and so on.      

However, in your example, l is of the type INT and >> operator is not overloaded for the INT data type. Of course INT is your custom class and the standard C++ library implementation could not know of it while implementing overloads for >> and hence no overload exists for it.
Since there is no overloaded version of >> available, you cannot directly use >> and the compiler will return an error of no matching function.      

You will have to provide an overload >> operator as an free standing function, receiving an INT, to be able to use cin>>l.

std::istream& operator>>(std::istream& is, INT& obj) 
{

}
Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • is it actually returning the object ? I guess in my code its returning just private member `i of INT ` class . My doubt is mainly about can we take object members value like `cin >> l;` as declared in main (). Also what if i have 2 or three different private integer value . What in that case ? – Invictus Feb 25 '12 at 14:05
  • 1
    @Ritesh: In that case, you would write a custom stream extraction and insertion operator for the class. – Linuxios Feb 25 '12 at 14:11
  • @Als What is the use of such conversion . Are thy helpful in practical code? – Invictus Feb 25 '12 at 14:11
  • @Ritesh: I have updated to answer all your queries, hope that helps. – Alok Save Feb 25 '12 at 14:26
  • "One implicit conversion is always allowed" -- this part is not really correct, since it's one implicit conversion *sequence*, which may either be a standard conversion sequence or a user-defined conversion sequence.. but for someone who doesn't know what conversion operators are, this may be a bit too much language lawyering. :) – Xeo Feb 25 '12 at 14:29
  • @Als Thanks a lot Als . This solves all my issue. I wrote :- `std::istream& operator>>(std::istream& is, INT& obj) { obj.setvalue(); }` and `void setvalue() { cin >> i; cin >> k; }` and it worked fine and took the input of both the variables from console and displayed them too :) – Invictus Feb 25 '12 at 14:34
  • The setvalue is very ugly. Use friend function instead: http://en.wikipedia.org/wiki/Friend_function – SwiftMango Aug 31 '13 at 18:45