jeudi 20 octobre 2016

What does it mean when I assign {} to an already exsiting object?

#include <vector>
#include <queue>
using namespace std;
int main()
{
    vector<priority_queue<int>> vec;
    vec.push_back({});//compiles
    vec.push_back({1});//don't work 
    vec[0] = {};//compiles
    vec[0] = {1};//don't work 
    return 0;
}

priority_queue doesn't have a initializer list constructor .

But I can still assign a {} to it .

I think it means that I just constructed an empty priority_queue using the default constructor,and assigned it to the already exsiting priority_queue object .

But shouldn't that be something like this ?

vec[0] = priority_queue<int>{};//compiles
vec[0] = priority_queue<int>();//compiles

What does this acctuly mean ?And why does it work ? I just omitted the priority_queue part .

vec[0] = {};//compiles
vec[0] = ();//don't work 

That dosen't mean I can reinitialize my queue object at any time ,dose it ?

priority_queue<int> que{};
que = {};//compiles
que{};//don't work

Is {} here something like nullptr ?

{} is an empty object for everything like nullptr is an empty pointer for every kind of pointers ?

priority_queue<int>* p{};
p = nullptr;
p = {};// still works

Aucun commentaire:

Enregistrer un commentaire