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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:43:46+00:00 2026-05-15T17:43:46+00:00

I went over making my own copy constructor and it overall makes sense to

  • 0

I went over making my own copy constructor and it overall makes sense to me. However, on the topic of doing your own assignment operator I need someone to fill in the blank for me.

I pretty much don’t get why you are returning *this in all the examples, such as the one below:

Foo & Foo::operator=(const Foo & f)
{

//some logic

return *this;

}

So if I have some statements like:

Foo f;
f.hour = 7;

Foo g;
g = f;

Once the assignment operator runs, it returns a reference to the g object (the *this). So now the question is, won’t I now have a statement implicitly like this?:

g = g (g being a reference)

The thing is, before, setting a reference to just an object would have caused the copy constructor to be invoked. In this case, it doesn’t even though it fits the signature of the copy constructor.

  • 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-15T17:43:46+00:00Added an answer on May 15, 2026 at 5:43 pm

    You want to return *this so you can chain =:

    Foo f, g, h;
    
    f = g = h;
    

    This is basically assigning h into g, then assigning g (returned by return *this) into f:

    f = (g = h);
    

    Another situation this is sometimes used in is having an assignment in a conditional (considered bad style by many):

    if ( (f = 3).isOK() ) {
    

    With the statement g = f; the return is just ignored, like if you did 3 + 4;.

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

Sidebar

Related Questions

Re-Edited: I went over to W3schools and played around in there javascript editor window
All the examples that I have read have only went over how to create
So I went over this block of code several times in a book that
So I went over the instructions in question How to build a Meteor package
I was just reading up a lecture which breifly went over Container Interface. I
I went over to the subsonic website and downloaded the version 3.0.0.4. My project
I went over many examples of this method and still cant figure out whats
i went over some questions and searched google a bit, but i couldnt find
in class we went over the subset_of/2 predicate that my teacher gave as follows:
Dealing with an employee that went over my head

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.