why copy constructor is called when passing temporary by const reference?

前端 未结 3 1770
后悔当初
后悔当初 2020-12-05 14:24

I am passing an unnamed temporary object to a function defined with const ref parameter. The copy ctor of the class is private, and I get a compilation error. I don\'t under

3条回答
  •  心在旅途
    2020-12-05 14:37

    Because a(1) calls the constructor A(int i) and then A(const A&) is called in the call to void f(const A&).

    Make the A(int i) constructor explicit and you should not face this error.

    Edit: I think i misunderstood the question. I might delete this.

提交回复
热议问题