Ctor Initializer: self initialization causes crash?

后端 未结 5 1094
我寻月下人不归
我寻月下人不归 2021-01-22 10:18

I had a hard time debugging a crash on production. Just wanted to confirm with folks here about the semantics. We have a class like ...

class Test {
public:
  Te         


        
5条回答
  •  囚心锁ツ
    2021-01-22 10:55

    This is the same difference as between

    std::string str;
    str = str;
    

    and

    std::string str(str);
    

    The former works (although it's nonsense), the latter doesn't, since it tries to copy-construct an object from a not-yet-constructed object.

    Of course, the way to go would be

    Test() : m_str() {}
    

提交回复
热议问题