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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:42:22+00:00 2026-06-01T18:42:22+00:00

class sample { private: int radius; float x,y; public: circle() { } circle(int rr;float

  • 0
class sample
{
  private:
    int radius;
    float x,y;
  public:
    circle()
     {

     }
    circle(int rr;float xx;float yy)
     {
      radius=rr;
      x=xx;
      y=yy;
     }

 circle operator =(circle& c)
     {
      cout << endl<<"Assignment operator invoked";
      radius=c.radius;
      x=c.x;
      y=c.y;
      return circle(radius,x,y);
     }


}

int main()
{
 circle c1(10,2.5,2.5);
 circle c1,c4;
 c4=c2=c1;
}

In the overloaded ‘=’ function the statements

radius=c.radius;
x=c.x;
y=c.y;

itself make all of c2’s data members equal to c1’s , so why is a return necessary?
Similarly, in c1=c2+c3, c2 and c3 are added using an overloaded + operator and the value is returned to c1, but doesn’t that become c1=, so shouldn’t we be using another = operator to assign the sum of c2 and c3 to c1? I’m confused.

  • 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-01T18:42:23+00:00Added an answer on June 1, 2026 at 6:42 pm

    It’s not needed (i.e. a void return type is legal), but standard practice is to return a reference to *this to allow assignment chaining without any efficiency overhead. E.g.:

    class circle
    {
        int radius;
        float x, y;
    
    public:
        circle()
          : radius(), x(), y()
        { }
    
        circle(int rr, float xx, float yy)
          : radius(rr), x(xx), y(yy)
        { }
    
        circle& operator =(circle const& c)
        {
            std::cout << "Copy-assignment operator invoked\n";
            radius = c.radius;
            x = c.x;
            y = c.y;
            return *this;
        }
    };
    
    int main()
    {
        circle c1(10, 2.5f, 2.5f);
        circle c2, c3;
        c3 = c2 = c1;
    }
    

    Returning a new object by value, as you’re doing, is certainly non-standard, as it creates unnecessary temporaries.

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

Sidebar

Related Questions

I have a Table: public class AppointmentTableModel extends AbstractTableModel { private int columns; private
Heres some sample code, class Base { private int val; Base() { val =
If I've a class like this, class Sample { private: int X; }; Then
Suppose you have a simple class like this: class foo{ private: int* mData; int
<?php //5.2.6 class Sample { private function PrivateBar() { echo 'private called<br />'; }
Consider this sample class, class TargetClass { private static String SENSITIVE_DATA = sw0rdfish; private
Simple class public class Group { public Int16 ID { get; private set; }
Simple WebSevice @WebService @Stateless public class HistoryFormatterWebService implements IHistoryFormatterWebService { @EJB private IHistoryFormatter historyFormatter;
template <typename T> class Table { public: Table(); Table(int m, int n); Table(int m,
I have wired experience with reflections. At first some sample code: public abstract class

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.