Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:23:27+00:00 2026-05-30T10:23:27+00:00

I came across this code just few minutes back here in Stack Overflow. I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:23:29+00:00Added an answer on May 30, 2026 at 10:23 am

    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) 
    {
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just came across this code and a few Google searches turn up no
I just came across this in some code, and I'm rather confused. timer =
I just came across a piece of code written by my ex-colleague few years
Possible Duplicate: curly braces in string Just came across this piece of code and
just wondering if someone else came across this. I got this piece of code
I just came across this code on the Mozilla site and, while to me
I was just browsing Sizzle's source code and I came across this line of
I just came across this code that lets me plot multiple ordinates (I know
I just came a cross this nice code that makes this scatter matrix plot:
I came across this code today AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null); Is there anything wrong with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.