vendredi 1 février 2019

difference b/w () and {} when creating objects c++?

Is there any difference in initializing the object with () and {} ?

A simple program

#include <iostream>
#include <string>
using namespace std;

class Demo {
    int n;
public:

Demo(int x): n{x}{
    cout << "Object is initialized by a number: " << n << endl; 
    }
};


int main()
{
   Demo a(4);
   Demo b{3};
}

the output of Demo a(4) and Demo b(3) works same but is there any difference b/w them and which one is efficient to use?

Aucun commentaire:

Enregistrer un commentaire