Copy constructor may not be called due to the auto-optimization of compilers. It’s quite compiler specific. See code as follows.
#include <iostream>
using namespace std;
class Point {
public:
Point(int a = 0, int b = 0) : x(a), y(b) {
//note that only in VS 2019, when we want print address, it won't print Ox, while Unix-like does print
cout << "Constructor called --> P(" << x << ',' << y << ")" << "\t with address --> 0x" << this << "\n\n";
}
Point(const Point& A) : x(A.x), y(A.y) {
cout << "Copy constructor called --> P(" << x << ',' << y << ")" << "\t with address --> 0x" << this << "\n\n";
}
// try to umcomment move constructor/assignment
/*Point(Point&& A) noexcept :x{ 0 }, y{ 0 } {
swap(x, A.x); swap(y, A.y);
cout << "Move constructor called --> P(" << x << ',' << y << ")" << "\t with address --> 0x" << this << "\n\n";
}
Point& operator=(Point&& A)noexcept {
if (this == &A) return *this;
swap(x, A.x); swap(y, A.y);
cout << "Move assignment called --> P(" << x << ',' << y << ")" << "\t with address --> 0x" << this << "\n\n";
return *this;
}*/
Point& operator=(const Point& A) {
x = A.x; y = A.y;
cout << "Copy assignment called --> P(" << x << ',' << y << ")" << "\t with address --> 0x" << this << "\n\n";
return *this;
}
~Point() { cout << "Destructor called --> P(" << x << ',' << y << ")" << "\t with address --> 0x" << this << "\n\n"; }
friend Point operator+(const Point A, const Point B) {
// try them respectively and see what's going on
//return Point(A.x + B.x, A.y + B.y);
Point C(A.x + B.x, A.y + B.y);
return C;
}
friend ostream& operator<<(ostream& os, const Point& A) {
os << "(" << A.x << ',' << A.y << ")";
return os;
}
private:
int x, y;
};
int main()
{
Point A(1, 2);
Point B(8, 9);
// try them respectively and see what's going on
Point C;
C = A + B;
//Point C = A + B;
cout << C << "\n\n";
return 0;
}
And these are the results tested in VS2019.
Tested in cpp.sh
Also very interesting to note that the order of copy assignment called in main function & that of destructors in which objects constructed by parameters and locals constructed in subroutine operator+, varies in differing systems.
Okay, now I’d like to make every April 13 my Thanksforgiving Day, one day for appreciating what I’ve got from God – my dear family, my awesome friends, cool teachers, etc; as well as for forgiveness which then includes two aspects – asking for forgiveness and forgiving others.
Alright, I posted my awesome Lucky Calendar before, ’cause I really couldn’t wait to share it the moment I thought of this amazing picture when my roomie referred April. Well, this picture was sent by one of my best female friends Alison on some particular day of April 2016 which I couldn’t recall exactly, but it seemed to be one day before the circled one, i.e. April 13. Actually, I am pretty much like number 13, since it is a prime number and remains more mysterious to me. What a coincidence is that today is April 13, which is also the day when I think I completely get out of my gloomy depraving awful mood induced by the relationship stuff. It turns out that I am now feeling quite released – the unresolved Mark-Ania issue finally gets done.
I made it, man. I am amazing! A span of 12 days starting from April 1, it was really bad times, but not all that bad. I was anxiously waiting for Ania’s reply to my stupid confession of love, which should never happen. I had known that she has a boyfriend, but why should I still do this crazy thing. What a jackass! However, “Stupid is as stupid does”, my uneasy heart finally gets back to the feet, plus as how i met your mother says maybe it was fated and maybe she is just not the one, or as Season 4 Ep 23 that Stella tells Ted that – I know that you are tired of waiting, and you may have to wait a little while more, but she’s on her way, Ted. And she’s getting here as fast as she can.
And now I think Ania did make the right decision just as Stella does – I may have to wait a little while more, but she’s on her way. And she’s getting here as fast as she can.
I like Ted, he’s cute, smart, funny, caring, sweet, generous & forgiving, open & straightforward, optimistic, and romantic. He encounters a lot of relationships while seeking for the one – The Mother of how i met your mother. But fate seems to go against him, leaving tons of obstacles on his way finding the one, which on the contrary merely renders Ted braver and more unbreakable.
Life is so long, we shouldn’t make ourselves so painful & depressed, we need to move on, see the bright side, be optimistic, focus on our work, and create a brilliant future. That’s all what a real man should do.
Okay, now let’s go to the topic, about why I call it my Thanksforgiving Day.
For one thing, I really felt much better after finishing watching HIMYM (how i met your mother) season 2, 3, 4, and a couple of episodes of season 5 (of course, skipped some episodes or content). Alright, I was crazy – from Apr 12 at 8 pm to 8:30 pm on the next day, excepting sleeping hours 3:30 am ~ 9:30 am, I was all watching this sitcom, and I almost thought that Ted finds the one on the last episode of Season 3, ’cause he says when the car accident occurred, the only person crossed his mind was Stella. But it wasn’t. More tragically, Stella runs away with her ex Tony at her wedding with Ted. Pool Ted! He really shouldn’t invite Tony to their wedding. So, the lesson is NEVER INVITE AN EX TO YOUR WEDDING.
Of course, Ted is very disappointed, mad, & angry. But he just doesn’t manifest his feelings, and he even tries to avoid meeting Stella, which considerably shrinks the gang’s scope of activities. Still, they run into Stella at a restaurant, and Ted’s friends try to persuade Ted to talk to her, relieve his rage to her, and behave like a man. When Ted follows Stella to the front steps of Tony’s house, he sees Tony & their lovely daughter Lucy welcome Stella home, hugging affectionately and then walked into the house. What a happy family, Ted thinks. Afterward, all his anger has gone. Unbelievable! It probably is the magic power of love and forgiveness, which is amazingly rare in modern society – People now are just too mean or afraid to give love & forgiveness to those who have ever hurt them before.
Ted just sets a stellar example of forgiving to all of us. He’s awesome. And apparently forgiving will make people happier, for hating others is really an energy-cost expensive job, rendering people exhausted both mentally & physically. We really don’t have to do that.
Meanwhile, the hardest thing for some people (at least me) is to ask for forgiveness, which means sometimes we have to put aside all of our dignity, pride, and self-esteem in order to give a heartfelt apology, I mean, over the phone or face to face or something. Sounds not that hard, but no way, goddamn tough, especially when the one you have to face is a dude. God, you totally have no idea what to say, how to start. Awful!
But I think I must learn to nail it, ’cause it’s kinda an essential skill which can booster the friendship. More importantly, it simply makes you feel good once you tell them how sorrowful & guilty you are for the stupid things you’ve done that ever hurt their feelings.
Okay, I need to put more enforcement into this, ’cause I think I really did a bunch of idiotic things that might hurt my friends’ feelings, and I always feel horrible for those things. Maybe I should just confess to them, it shouldn’t be that hard, maybe.
Somehow, this evening I suddenly felt an intense feeling of belonging to sea, to the Nature. It kinda like the voice of my soul in response to the call of the Nature, in a way, I now find the feeling is extremely like the one of Tristan (a role in Legends of the Fall acted by Brad Pitt) that his running wild horse in his mind wants to seek for some inner peace, for he is plagued with guilt over Samuel’s death and feels responsible for driving Alfred away after he returns home.
The feeling, however, didn’t last very long, say about an hour (maybe 40 minutes), but it was amazingly fantastic – I just wanted to go wild, be wild about the sea, the ocean, the mountain, the cloud, the sky, the sun, everything, everything in the Nature. Then gone, leaving a piece of calm and peace, like the water in the lake, so peaceful.
(By the way, these beautiful images are downloaded from Pixabay, you can find a tremendous number of stunning free images there.)