Why copy constructor not invoked here

后端 未结 2 706
长情又很酷
长情又很酷 2021-01-13 23:37
stri(){}

stri(char *s);//constructor used to initilize object with constant string

stri(stri &s1);//copy constructor performs memberwise copy

friend stri oper         


        
2条回答
  •  旧巷少年郎
    2021-01-14 00:06

    Let me dust off my crystal ball and guess that the error you're getting is somewhere along the lines of "temporary cannot bind to reference." That's because your copy constructor takes its parameter as stri & instead of const stri &; in other words, a reference to non-const. Such references cannot bind to temporaries. s1 + s2 returns a temporary, so the copy ctor invocation fails.

    Unless you're doing really whacky stuff in your copy ctor (modifying the object copied from), change it to take its parameter as const stri &.

提交回复
热议问题