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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:54:18+00:00 2026-05-16T11:54:18+00:00

I would like to overload << operator for chaining like below function1(param1, param2)<< obj2

  • 0

I would like to overload << operator for chaining like below

function1(param1, param2)<< obj2 << obj3 << string4

function1 would return an object.

What I want to do is after string4, I need to call a function using param1, param2.

My questions will be

  1. How do I know string4 is the last parameters in the expression and hence I need to call another function2 using param1 and param2 or it isn’t possible to do so?

  2. How do I pass param1 and param2 to the function to be called? I could not store the param1 and param2 in the object as it is a multithreaded.

Thanks.

  • 1 1 Answer
  • 2 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-16T11:54:19+00:00Added an answer on May 16, 2026 at 11:54 am

    You can return a helper object from function1 by value which calls function2 in it’s destructor.

    Consider this example:

    #include <iostream>
    
    using namespace std;
    
    void function2( int i, int j ) {
        cout << "function2 called with " << i << " and " << j << endl;
    }
    
    struct Function2Caller {
      Function2Caller( int param1, int param2 ) : m_param1( param1 ), m_param2( param2 ) {}
      ~Function2Caller() { function2( m_param1, m_param2 ); }
    
      int m_param1, m_param2;
    };
    
    Function2Caller &operator<<( Function2Caller &obj, int x ) {
        cout << "Streaming " << x << endl;
        return obj;
    }
    
    Function2Caller function1( int i, int j ) {
        cout << "function1 called with " << i << " and " << j << endl;
        return Function2Caller( i, j );
    }
    
    int main() {
        function1( 2, 3 ) << 4 << 6;
    }
    

    This program prints

    function1 called with 2 and 3
    Streaming 4
    Streaming 6
    function2 called with 2 and 3
    

    The idea is that at the end of your line, the Function2Caller object goes out of scope and the destructor then does its work.

    Please note that when implementing this you should probably forbid copying of Function2Caller objects and make function1 the only one who can call the Function2Caller constructor.

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

Sidebar

Related Questions

I have typedef std::string OrderID; I would like to overload the operator ++ for
I would like to overload the (/) operator in F# for strings and preserve
I would like to overload the operator<< to allow it to work with shared_ptr
I would like to overload the + operator of iterator in list class, something
I'm trying to overload the assignment operator and would like to clear a few
Is there a way to overload ->* for use with a smart-pointer-like object? Below
I would like to overload a method with the @QueryParam but everytime I try
I have a specific class C and I would like to overload some math
Instead of having an add() method in the repository, I would like to overload
Is it possible to overload the index operator in Scala? If yes, what would

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.