C++ initialization strange behavior

typedef struct node {
    
    int val;
    int val2;
    
    node(int a, int b) : val(a), val2(b) {}
    node(int val) = delete;
}node;

int main()
{
    node a = {3};
    cout << a.val << " " << a.val2 << endl;
    return 0; 
}

The above code gives compile error showing that use of deleted function node::node(int).

However, when I remove node(int a, int b) : a(val), b(val2) {}, this code compiles without a problem. How would this happen?

Source: Windows Questions C++

LEAVE A COMMENT