C ++ constructor implicit conversion does not occur

I defined class A

class A{
public:
  A(int){}
private:
  A(const A&);
  A& operator=(const A&);
};

I thought that since I give the constructor from int, implicit construction is provided ... Anyway, for now

A myA(7);

works fine, g ++ gives me this line:

A myA = 7;

following error:

Test02.cpp: In functionint main(int, char**)’:
Test02.cpp:5:3: error: ‘A::A(const A&)’ is private
Test02.cpp:12:12: error: within this context

Another compiler likes this conversion. Where is the truth? How should I define A to get A myA = 7; work?

+4
source share
2 answers

GCC is right. A myA(7);- direct initialization - initializes myAusing a constructor that takes a parameter int.

A myA = 7; - - A int, myA . , , , . sinec , .

A myA = 7;, ( ), .

+3

g++ . ,

A myA = 7;

- . , A RHS, . , .

,

A myA(7);

- . A(int).

.

+3

Source: https://habr.com/ru/post/1524788/


All Articles