gpt4 book ai didi

c++ - 转换指南

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:08 25 4
gpt4 key购买 nike

如果您有两个类 A 和 B,并且您需要定义从 A -> B 和 B -> A 的转换,那么标准解决方案是什么?

  1. 让 A 有一个构造函数接受 B 和 A 有转换运算符到乙?
  2. 让 A 和 B 都拥有对方的构造函数?
  3. 让 A 和 B 都具有到另一个的转换运算符?
  4. 某种 CPO convert<To>(From)
  5. 还有其他办法吗???

(注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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com