PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 6, 2022

[FIXED] Why does my operator + overload call my copy constructor despite being passed by reference?

 July 06, 2022     c++, class, operator-overloading, pass-by-reference     No comments   

Issue

I've done some research, but I have not been able to find an explanation for the behavior of my code (I may not be asking the right questions). I'm working in C++, using VS2019.

The code regards the Points class found in the Data Structures and Other Objects textbook.

My class as it exists now:

class point {
private:
    double x;
    double y;
public:
    //constructors
    point(double ix = 0.0, double iy = 0.0) {
        x = ix;
        y = iy;
    }
    point(const point &z) {
        x = z.getX();
        y = z.getY();
        cout << "copy constructor triggered" << endl;
        cout << x << "," << y << endl;
    }

    //assignment
    void operator = (const point& result) {
        x = result.getX();
        y = result.getY();
        cout << "assignment overload triggered" << endl;
        cout << x << "," << y << endl;
    }

    //getters
    double getX() const {
        return x;
    }
    double getY() const{
        return y;
    }

    //setters
    void setX(double newX) {
        x = newX;
    }
    void setY(double newY) {
        y = newY;
    } //other function members are omitted
}//end point() class

point operator + (const point& A, const point& B) { //Adds x1 to x2, y1 to y2
    point n;

    n.setX(A.getX()+ B.getX());
    n.setY(A.getY()+ B.getY());
    return n;
}

The problem first arose when I found that I could not create an instance of a class on the same line as I tried to add two instances using the operator+ overload.

point p1(5, 3);
point p2(12, 9);
point p3 = p1 + p2; //gives the following error: class "point" has no suitable copy constructor

I found I had made an error by forgetting to include "const" in my copy constructor. By resolving that issue with the copy constructor I was able to get the code working well enough to submit my assignment, but I wasn't satisfied with my understanding.

In the process of resolving that issue, I'd also added the assignment operator overload (=) inside the class definition. Running through some console debugging, I found a strange pattern:

point p1(5, 3);
point p2(12, 9);
point p3;
p3 = p1 + p2; //calls both copy constructor and assignment instructor
point p4 = p1 + p2; // calls only copy constructor

point p5;
p5 = p1; //calls only assignment operator
point p6 = p2; // calls only copy constructor

So now, I'm wondering why p3 = p1 + p2 calls both the copy constructor and the assignment overload. Is the compiler calling the copy constructor when adding the points? I didn't think it would be since the operator+ overload creates a temp point with the default constructor then manually changes the values with getters and setters, then returns that temp point. My understanding is that the copy constructor should only be called when creating a new object, which p3 is not.

I should also say that we have not yet discussed using pointers with classes/objects.

Any help here is appreciated. Thanks in advance for any assistance.


Solution

Creating a temporary point and changing its values will not call the copy constructor.

You are returning an object here, not a pointer or a reference to an object:

point operator + (const point& A, const point& B) { //Adds x1 to x2, y1 to y2

when you return from your function a new object is created from the one that you had inside your function and this is where the copy constructor gets called.



Answered By - bhristov
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing