为什么"out variable pattern"在 C++ 中如此频繁地使用?
Why is the "out variable pattern" used so often in c++?
我不知道模式的实际名称
我通常不写c++代码,但每次我读它时,我都会看到相同的模式一遍又一遍。
我刚刚在虚幻引擎4中又发现了它,看起来像这样
FVector CameraLoc;
FRotator CameraRot;
GetActorEyesViewPoint(CameraLoc, CameraRot);
我一直很讨厌这种模式,因为我永远不知道哪个参数被改变了,然后有时函数期望对象被正确初始化。
为什么要使用这个模式?把它包装在一个结构体中不是更好吗?
struct ActorEyesViewPoint {
FVector CameraLoc;
FRotator CameraRot;
};
和
ActorEyesViewPoint GetActorEyesViewPoint();
此模式用于防止从函数返回时不必要的对象复制。这基本上是RVOs的显式版本(http://en.wikipedia.org/wiki/Return_value_optimization)
注意,在c++ 11中不再需要移动语义
这种模式的一个优点是可以将派生类传递给接受基类的函数。
如果你有
class A { };
class B : public A { };
void foo(A* a);
A bar();
B
类的对象,foo
是有用的,但bar
不是。
相关文章:
- C++ Singleton - Prevent ::instance() to variable
- IN, OUT, INOUT Parameters
- 即使使用调试编译标志,表达式也是"optimized out"
- C++双重释放或损坏(out)
- 将 out/in out 参数与 if/switch 的 init 语句一起使用
- 如何通过多类"Union variable" (sfml) 使用轮询事件
- 在命令行上将输入显示到数组中时如何删除 ./a.out?
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 为什么我会收到"Run-Time Check Failure #2 - Stack around the variable 'pr' was corrupted"错误?
- C++:寻找"returning address of local variable..."的更正
- 为什么我的向量::擦除调用会抛出"vector subscript out of range"?
- 解决方案在第 25 行执行错误'out of bounds'
- C++ 向量加减抛出"expression: vector subscript out of range."错误
- C++ - 在我尝试制作一个简单的计算器时有一个"uninitialized local variable y used"警告
- Visual Studio Code "variable " u8 的 C/C++ 扩展名 " " 不是类型名称"
- 对函数的 out 字符串参数使用 swap 与赋值
- C++ "Using Uninitialized Memory.. (variable name) "
- 运行编译的 a.out 后出现分段错误(核心转储)错误
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- 为什么"out variable pattern"在 C++ 中如此频繁地使用?