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 4014276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:28:38+00:00 2026-05-20T09:28:38+00:00

I’m making a simple class that uses operator<< . It will store two parallel

  • 0

I’m making a simple class that uses operator<<. It will store two parallel arrays of data, each with a different (but already-known) datatype. The idea is that the final interface will look something like this:

MyInstance << "First text" << 1 << "Second text" << 2 << "Third text" << 3;

Which would make the arrays look something like this:

StringArray: | "First text" | "Second text" | "Third text" |
IntArray:    | 1            | 2             | 3            |

I can handle the logic of checking the input to make sure everything matches up, but I’m confused with the technical details of operator<<.

The tutorials I checked say to overload it as a friend function with an std::ostream& return type, but my class has nothing to do with streams. I tried using void as the return type but got compilation errors. Eventually I ended up with returning a reference to the class, but I’m not sure why that works.

Here is my code so far:

class MyClass
{
public:

MyClass& operator<<(std::string StringData)
{
    std::cout << "In string operator<< with " << StringData << "." << std::endl;

    return *this; // Why am I returning a reference to the class...?
}

MyClass& operator<<(int IntData)
{
    std::cout << "In int operator<< with " << IntData << "." << std::endl;

    return *this;
}
};

int main()
{   
MyClass MyInstance;
MyInstance << "First text" << 1 << "Second text" << 2 << "Third text" << 3;

return 0;
}

Additionally, the user of my class can do something like this, which is unwanted:

MyInstance << "First text" << 1 << 2 << "Second text" << "Third text" << 3;

What can I do to enforce the alternating nature of the input?

  • 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-20T09:28:38+00:00Added an answer on May 20, 2026 at 9:28 am

    The reason ostream operators return a reference to the ostream, and the reason it somewhat helped your case to return a reference to MyClass is that an expression like A << B << C is always interpreted like (A << B) << C. That is, whatever the first overloaded operator returns becomes the left-hand side of the next operator call.

    Now, if you want an expression like MyInstance << "First text" << 1 << 2 to produce a compiler error, you need to make sure that the type returned after the first two << operators is a type that cannot be called with another int. I think something like this might do what you want (thanks to @Pete Kirkham for a good improvement idea):

    struct MyClass_ExpectInt;
    class MyClass {
    private:
        friend MyClass& operator<<(const MyClass_ExpectInt&, int);
        void insert_data(const std::string& StringData, int IntData);
        // ...
    };
    struct MyClass_ExpectInt {
        MyClass& obj_ref;
        std::string str_data;
        explicit MyClass_ExpectInt( MyClass& obj, const std::string& str )
          : obj_ref( obj ), str_data( str ) {}
    };
    MyClass_ExpectInt operator<<( MyClass& obj, const std::string& StringData )
    {
        // Do nothing until we have both a string and an int...
        return MyClass_ExpectInt( obj, StringData );
    }
    MyClass& operator<<( const MyClass_ExpectInt& helper, int IntData )
    {
        helper.obj_ref.insert_data( helper.str_data, IntData );
        return helper.obj_ref;
    }
    

    Those would be the only two overloaded operator<< functions you define related to MyClass. This way, every time you call an operator<<, the compiler switches the return type from MyClass& to MyClass_ExpectInt or vice-versa, and passing the “wrong” sort of data to operator<< is never allowed.

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

Sidebar

Related Questions

No related questions found

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.