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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:13:42+00:00 2026-06-15T05:13:42+00:00

Is it safe to write our own copy constructor always during operator overloading ?

  • 0

Is it safe to write our own copy constructor always during operator overloading ?

Complex Complex::operator+(const Complex&  other)
{
    Complex local ;
    local.result_real = real + other.real;
    local.result_imaginary = imag + other.imag;
    return local ;
}

Most of the times i have seen the above format , instead of returning it as reference .

Can we take thumb rule like
1- Always pass the function parameter by reference .
2- Always return the object by reference .

Is there any special case in operator overloading where we have to return the object by value only ?

  • 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-06-15T05:13:43+00:00Added an answer on June 15, 2026 at 5:13 am

    Can we take thumb rule like 1- Always pass the function parameter by reference . 2- Always return the object by reference .

    No, not really. Even if you found a way to return a non-dangling reference, an operation such as addition should not return a reference, since this does not make sense. a+b should return a new object, while for a+=b it does make sense to return a reference to the LHS.

    Concerning passing an argument by reference, there is no rule of thumb either. Consider this:

    A operator+(const A& lhs, const A& rhs)
    {
      A tmp = lhs; // explicitly make a copy
      return tmp += rhs;
    }
    

    and this:

    A operator+(A lhs, const A& rhs)
    {
      return lhs += rhs;
    }
    

    The second version passes one argument by value, resulting in simpler code and giving the compiler more opportunities to elide temporary copies.

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

Sidebar

Related Questions

I want to make thread-local buffer for strerror_r call and write my own thread-safe
In JPA/Hibernate , we can write type-safe queries and accumulate our query predicates one
Can anyone point me to a thread-safe implementation of the Copy-on-write (COW) idiom? The
Is the following code safe? class B { public: int& b; B (int& _b)
Is it safe to write data to an NSPasteboard object from a background thread?
Instead of using UIImageWirteToSavedAlbum, is it safe to write your image to /private/var/mobile/Media/DCIM/100APPLE/ directly?
I'd like to write a safe version of toEnum : safeToEnum :: (Enum t,
I would like to have thread-safe read and write access to an auto-implemented property.
I'm trying to write a thread-safe Map[K, Set[V]] implementation in java. If a unique
Is there any process safe logging library which can write same file from multiple

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.