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

  • Home
  • SEARCH
  • 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 6733255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:44:49+00:00 2026-05-26T10:44:49+00:00

I want to overload the >> operator in c++ so that my class of

  • 0

I want to overload the >> operator in c++ so that my class of vectors can take input in the form [1 2 3 4 5].

For example:

class A
{
public:
    A()  {} 

    A(const std::vector<int>& source)
      : v_(source)  
    {

    }

    // returns an immutable reference to v_
    const std::vector<int>& get_v() const
    {
       return v_;
    }

    // returns a mutable reference to v_
    std::vector<int>& get_v()
    {
        return v_;
    }
private:
    std::vector<int> v_;
};

int main (){
A my_vector;

while (cin >> my_vector)
   // do some other computations

}

Right now I’ve declared the overloading function for >> like this:

istream& operator>> (istream & stream, A vector){

}

But I’m confused on what I need to do next to store what ever is in my cin to my A object. I assume I need to check for both [] in the input to make sure it is accepted otherwise exit the application. Any suggestions

  • 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-26T10:44:50+00:00Added an answer on May 26, 2026 at 10:44 am

    The first thing that you need is to pass the second argument by reference, so that the object modified inside the operator is the same object that you are reading into.

    The next thing that you need is to actually read from input into your internal vector. For that You need to skip the first [, and then read the numbers until you find the ].

    The first character can be read into a char, then you can use copy to fill in the vector, and finally another read into a char to read the ]:

    std::istream& operator>>( std::istream & stream, A & vector ) {
        char ch;
        stream >> ch; 
        std::copy( std::istream_iterator<int>(stream), std::istream_iterator<int>(),
                   std::back_inserter( vector.get_v() ) );
        stream.clear();
        stream >> ch;
        return stream;
    }
    

    Now, this is a sketch, and you will need to handle errors, in particular what happens if the first character is not [ or the last character is not ]…

    Side notes:

    It is usually not a good idea to provide full access to your internal data, as you are doing with the get_v() method. That is, there is no point in making v_ private if you are just going to provide full access… if it needs to be accessible make it public and you can avoid having to write the two accessor functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to overload the operator , so that I can assign my fraction
I want to overload the operator + in a class which has a vector
I want to overload the + operator for adding segments together to form a
I have a Card class and I want to overload the > operator to
I want to overload the * operator in python. In C++, you can overload
I want to overload the receiver operator so I can do this: someClass >>
I'm writing a collection class. I want to overload the square brackets operator ([])
I want to overload new operator in a template class. But something wrong happends.
I have a class that uses a struct, and I want to overload the
I want to overload the assignment operator for types like int, long etc. That

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.