2
#include "stdafx.h"
#include "iostream"
using namespace std;

class complex
{
    int real;
    int img;
public:
    complex(int x = 0, int y = 0)
    {
        cout << "Inside Prama" << endl;
        real = x;
        img = y;
    }
    complex operator+(complex x)
    {
        complex temp;
        temp.real=real + x.real;
        temp.img=img + x.img;
        return temp;
    }
    void display()
    {
        cout << real << ' ' << img << endl;
    }
};

int main()
{
    class complex c1(5,6), c2(7,8),c3(7,7),c4;
    c1.display();
    c2.display();
    c3.display();
    c4 = c1+c2+c3;
    c4.display();
    return 0;
}

for above operator overloading code following is the output:

enter image description here

why that parameterized constructor is called at the time of adding of the objects.I am not getting the reason for that

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Shujaul Hind
  • 105
  • 1
  • 9
  • 3
    `complex temp;` is creating a new object, using constructor. – Yksisarvinen Jul 29 '21 at 10:04
  • 1
    Further, it's unnecessary to even *have* `temp`. You already have a writable `complex` that you can freely change to your liking and return: the by-value parameter `x` you received as an operator argument. There are times when you can just exploit that; this is one of them. `x.real += real; x.img += img; return x;` for example. – WhozCraig Jul 29 '21 at 10:06
  • 1
    Should be `complex operator+(const complex& x) const` (silently implicit copy constructor is used). – Marek R Jul 29 '21 at 10:07
  • 1
    Please do not include text as image! never! Just copy paste content of console/terminal. https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question – Marek R Jul 29 '21 at 10:09
  • 1
    @WhozCraig though that inhibits nrvo, no? I once learned that its "better" to pass by ref and return a local copy to get nrvo – 463035818_is_not_an_ai Jul 29 '21 at 10:33
  • 1
    @463035818_is_not_a_number Oh no, it totally can. Somehow I doubt the OP would much care. Truth be told, I'd implement reference-proper `operator +=` as a member, and `operator +` as a free-function invoking the former and taking the first argument by value anyway, but I suspect both of those (dependent operators and nrvo) may be a bit higher in the atmosphere than the OP want's to go right now. – WhozCraig Jul 29 '21 at 10:39
  • 1
    @WhozCraig agree. I wasnt sure how to phrase an answer, but I guess I'll just keep it simple – 463035818_is_not_an_ai Jul 29 '21 at 10:41
  • @463035818_is_not_a_number It's been 0 days since you asked your last question. We ask that you wait 4 days before asking again. Use this time to revisit your previous questions, editing to address any issues that folks have pointed out in comments.This is the message i am getting now – Shujaul Hind Jul 29 '21 at 11:42

1 Answers1

2

Just summarizing comments...

Here

complex operator+(complex x)
{
    complex temp;
    temp.real=real + x.real;
    temp.img=img + x.img;
    return temp;
}

complex temp; calls the constructor and thats the output you see. Typically you'd pass x as const reference to avoid the copy, but as you need a copy anyhow you can use x:

complex operator+(complex x)
{
    x.real +=real;
    x.img +=img;
    return x;
}

This will only invoke the compiler generated copy constructor when you call the operator.


There are more subtleties to consider. Using x instead of making a local copy inhibits named return value optimization. Also it is common to implement operator+ in terms of operator+= and operator+ can be a free function then. Note that operator+= can be more efficient, because no copy is needed at all. For more details on operator overloading in general I refer you to What are the basic rules and idioms for operator overloading?


PS: "parametrized constructor" is a term I have seen only in poor misleading tutorials so far. It is not an official term. The relevant term here is default constructor. complex(int x = 0, int y = 0) is a default constructor because it can be called without parameters. It is a good example for why "parametrized constructor" does not convey lots of meaning and is rather misleading.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185