3
4

2 回答 2

4

const std::unique_ptr<B>类似于B* const,即指向可变对象的不可变指针B-not const B*,指向不可变对象的可变指针B。如果你想从unique_ptr版本中得到同样的错误,你需要写std::unique_ptr<const B>. 实际上,您正在返回unique_ptrbyconst引用,但B它所引用的不是const

于 2012-08-01T01:34:59.710 回答
0

You are returning a const reference from your getB() method. You cannot change the address to which a const reference points. Yet calling get() on the returned object would give you the pointer. That is not a good thing to try!

于 2012-08-01T01:38:10.757 回答