confusion on object construction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//neccessary headfiles are included

class obj{
    public:
    int x;
    obj(const &o){;}
    obj(){}//Q1 mark1
};

int main(){
    obj a;//Q1 mark2
    a.x=3
    obj b=a;//Q2 mark1
    cout<<a.x<endl;
    cout<<b.x<endl;//Q2 mark2
}

Q1:
why it doesn't work(Q1 mark2) when i delting default constructor(line on Q1 mark1)
Q2:
it finnaly cout:
3
16
i wonder why it was not "3 3" but "3 16".(Q2 mark)

otherwise, i'm interested in what complier do when constructing a object.Appreciate it if you can recommand me some artciles on it.
Thank a lot.

Last edited on
L11 creates a as an instance of obj using default constructor - as no specific constructor is specified. Hence there has to be a default constructor for obj which is L7.

L6 is the copy constructor - which does nothing. L13 calls the copy constructor for the assignment but as the copy constructor does nothing, b has the value of x that happens to be in the memory used for x as x isn't initialised.

Hence the code shows 3 nn where nn could be any value.

Note that the code above has syntax errors!
Topic archived. No new replies allowed.