如果您有两个类 A 和 B,并且您需要定义从 A -> B 和 B -> A 的转换,那么标准解决方案是什么?
- 让 A 有一个构造函数接受 B 和 A 有转换运算符 到乙?
- 让 A 和 B 都拥有对方的构造函数?
- 让 A 和 B 都具有到另一个的转换运算符?
- 某种 CPO
convert<To>(From)
- 还有其他办法吗???
(注2和注3需要循环依赖)
最佳答案
我在cppslack上问了一圈,好像没有标准/通用的情况,所以归结为这种情况。
(1) is what you do when you're extending an API, i.e. A knows about B but B doesn't know about A;
(2) or (3) is what you do when A and B are defined as part of the same API, i.e. they know about each other;
(4) is how you unify two unrelated APIs, i.e. neither A nor B knows about the other.
关于c++ - 转换指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58259002/