I don't understand when the constructor is called in this example

i don't care lyrics
justin bieber i don't care
i don't care i love it
i don't care live
i don't care song
ed sheeran
i don't care songs
i don't care apocalyptica

Hi I have been trying to analyze the following code that uses operation overloading.

#include <iostream>
using namespace std;

#define DBG(str) cout << str << endl

class Integer {
    int n;
public:
    Integer(int _n) : n(_n) { DBG("A"); };
    Integer(const Integer& i) : n(i.n) { DBG("B"); };

    Integer& operator=(const Integer& i) { DBG("C"); n = i.n; return *this; };
    Integer& operator+=(const Integer& i) { DBG("D"); n += i.n; return *this; };

    friend Integer operator+(const Integer& a, const Integer& b);
    friend Integer operator*(const Integer& a, const Integer& b);
    friend ostream& operator<<(ostream& os, const Integer& i);

};

Integer operator+(const Integer& a, const Integer& b) {
    DBG("E"); return Integer(a.n + b.n);
}
Integer operator*(const Integer& a, const Integer& b) {
    DBG("F"); return Integer(a.n * b.n);
}
ostream& operator<<(ostream& os, const Integer& i) {
    DBG("G"); os << i.n; return os;
}

int main() {
    Integer n1(1), n2(2);
    Integer n = 5 + n1 + 2 * n2;

    cout << n << endl;
}

and the result comes out to be...

A // constructor called when n1 is created
A // constructor called when n2 is created
A // when is this called?
A // when is this called?
F // called when 2 * n2 is operated
A // called when Integer(a.n * b.n) is created in the multiplication function
E // called when 5 + n1 is operated
A // called when Integer(a.n + b.n) is created in the addition function
E // called when (5 + n1) + (2 * n2) is operated
A // called when Integer is created in the addition function
G // called when n is printed using cout
5 // value of n

Now what I'm having trouble with the most is the third and fourth print of A. In the sentence Integer n = 5 + n1 + 2 * n2; the object n is being created and the right value is assigned to n, so a copy constructor should be called? What I think should happen is the constructor should be called for making a temporary object of (5 + n1 + 2 * n2) and then by copying it to n, the copy constructor should be called. What am I understanding wrong?

Could you please explain what is happening? Thank you in advance.

Problem is in this line:

Integer n = 5 + n1 + 2 * n2;

There is no operator+ for int and Integer, but compiler silently did a implicit conversion from int to Integer, since by default all single argument constructors can be used as conversion method. Here is your live code (improved a bit).

This is one of dangerous features of C++ so there is explicit keyword to be used before constructor. If you add it:

explicit Integer(const Integer& i) : n(i.n) { DBG("B"); };

Compiler will report an error since implicit conversion now can't be performed and there is no operator+(int, const Integer&) and operator*(int, const Integer&).

Ed Sheeran - I Don't Care (Live At Abbey Road), If you don't know how exactly magic feels like , listen this song. Read more. Show less. Reply 5�Duration: 5:30 Posted: Dec 6, 2019 Song I Don't F**k With You; Artist Big Sean; Licensed to YouTube by UMG (on behalf of Getting Out Our Dreams Inc. (G.O.O.D.) Music / IDJ); The Administration MP (Publishing), LatinAutor

Your operator+ and operator* functions receive an const Integer& as parameter.

When this line is calculated (5 + n1 + 2 * n2) 5 and 2 will be auto-converted to Integer.

If you want them to not be converted, you should consider creating operators for int as parameter.

EDIT: You can also use explicit constructor.

For example: explicit Integer(int _n){...}

//thanks for the comment @formerlyknownas_463035818

Ed Sheeran - I Don't Care (Lyrics) Ft. Justin Bieber, Ed Sheeran - I Don't Care (Lyrics) Ft. Justin Bieber. Popular Music.Duration: 3:38 Posted: May 9, 2019 50+ videos Play all Mix - Ed Sheeran & Justin Bieber - I Don't Care [Official Video] YouTube Ed Sheeran - South of the Border (feat. Camila Cabello & Cardi B) [Official Video] - Duration: 4:46.

Friend function is used in operator overloading only when the first parameter of the operator function is not an object of the target class for which the operator function is defined. In your case, Integer:

Integer operator+(const Integer& a, const Integer& b) {
    DBG("E"); return Integer(a.n + b.n);
}
Integer operator*(const Integer& a, const Integer& b) {
    DBG("F"); return Integer(a.n * b.n);
}

, the first parameter is indeed an object of type Integer which is your target class. What you probably want is to be able to use your Integer object as operands along with primitive integer types. Change your friend function signatures to:

Integer operator+(const int& a, const Integer& b) {
    DBG("E"); return Integer(a + b.n);
}
Integer operator*(const int& a, const Integer& b) {
    DBG("F"); return Integer(a * b.n);
}

Note that a is used instead of a.n because when an operator function is defined as friend, both operands are passed as parameters.(unlike non-friend operator functions where the first operand is a calling object). So the a contains a value of type int and not a value of type Integer.

The anonymous calls to the constructor containing DBG("A") is a result of the implicit conversions happening when 5 and 2 are being converted from int to Integer.

This is unreliable although your code may work. Hope this helps.

I Don't Care (Ed Sheeran and Justin Bieber song), and lost my job because of COVID. I don't have much in savings and feel lost. What can I do? Published: July 29, 2020 at 4:54 p.m. ET. By. Alessandra Malito. If I could do this, well, you could do that, but I don't dance! Watch Lucas Grabeel and Corbin Bleu perform “I Don't Dance” in the official music video from High School Musical 2! Show some

I'm 63, a widow and lost my job because of COVID. I don't have , In ethics, there are many different notions of privacy. Two are relevant here. First, there's informational privacy, which concerns what others don't� Download or Stream No.6 Collaborations Project - out now: https://atlanti.cr/No6 Tickets for the Divide tour here - http://www.edsheeran.com/tour Subscribe t

Can My Boss Make Me Promise I Don't Have Covid-19 Symptoms , "I don't wish this on anyone": Young single mom of three dies of COVID-19 (Photo Courtesy: Anadelia Diaz). WEST PALM BEACH, Fla. We would like to show you a description here but the site won’t allow us.

'I don't wish this on anyone:' Young single mother of three dies from , “If they just stayed off the fence and weren't being aggressive towards us, we'd just be inside twiddling our thumbs. … I don't think there's an end� Co-Founder of I Do Now I Don’t, Josh Opperman launched the revolutionary consumer-to-consumer Marketplace for recycled diamonds, jewelry, and watches together with his sister, Mara, in 2007.

Comments
  • Make your constructor print out the value of n to help trace what's happening.
  • Can you place a break-point on DBG("A") and check the call-stack?
  • Integer n = 5 + n1 + 2 * n2; The 5 is converted to an Integer object, which prints out A. Same goes for the 2
  • You should step through the code in a debugger to see what's going on. That said, my guess is that the third and fourth "A" markers come from converting 5 and 2 to Integer. You're right about the copy constructor that creates n.
  • You could try making your Integer(int _n) constructor explicit and see where the compiler points at errors.
  • Thank you for a clear answer. So if int is implicitly converted to Integer, what does i.n really mean? Can 5 have member variables?
  • Defining constructor which can be called with single argument by default compiler can use as conversion method. In such situation compiler creates temporary unnamed object, which will be auto destroyed after line was executed.
  • better yet to declare the constructor as explicit to prevent implicit conversions
  • Thank you. Helped me a lot.
  • Thank you all for great answers