Conversion Operators in C++

后端 未结 4 2007
眼角桃花
眼角桃花 2020-12-08 00:46

Please help me understand how exactly the conversion operators in C++ work. I have a simple example here which I am trying to understand, though it is not very clear how the

4条回答
  •  孤街浪徒
    2020-12-08 01:34

    Example exObject = theInt; // implicitly created copy constructor takes place
    // object implicitly created from int and then copied
    // it is like
    Example exObject = Example(theInt);
    // so it uses sequence
    // Example(int) -> Example(const Example&)
    int theInt1 = ctr; // operator int()
    

    If you compiler supports copy constructor optimization and return value optimization you won't notice

    Example(const Example&)
    

    execution, but you can declare copy constructor to be private to understand what I am talking about.

提交回复
热议问题