jeudi 15 avril 2021

Understand the constructor and the assignement c++

I’m a newbie in C++ and I come from C. The OOP is a whole new paradigm for me.

So I will ask a question on this stupid example:

#include <iostream>

class Object
{
public:
    int first;
    int second;
    Object ():first (0), second (0)
    {
        std::cout << "created1\n";
    }
    Object (int x, int y):first (x), second (y)
    {
        std::cout << "created2\n";
    }
};

int main ()
{
  Object s = { 1, 1 };  //Or  Object s(1,1);
  Object d;
  s = {2, 2};
  s = d;
  return 0;
}

/* Output:
    created2
    created1
    created2
*/

What are the differences in the two assignement (they are assignement?) before the return?

Thank you for the attention.

Aucun commentaire:

Enregistrer un commentaire