C++;修改参数和/或返回新实例的方法/函数的命名约定? 我想知道在编写这样的方法时,C++中是否有一个“被广泛接受的”命名约定: class Transform3d { public: // Apply rotation using a copy of toRotate and return it Vector3d ApplyRotation(const Vector3d& toRotate) const; // Apply rotation directly to toRotate void ApplyRotationInPlace(Vector3d& toRotate) const; // Apply rotation on this Transform3d instance (orientation quat) void ApplyRotation(const Vector3d& toRotate); //... // Apply transformation using a copy of toTransform and return it Transform3d ApplyTransform(const Vector3d& toTransform) const; // Apply transformation directly to toTransform void ApplyTransformInPlace(Vector3d& toTransform) const; private: Quaternion orientation; Vector3d position; }; P>是否有编写C++方法的准则,这些方法涉及参数修改和返回新实例(对参数没有修改)? < P>是的,你偶然发现了它。这是常量修饰符 < C++ >编写C++方法有一些指导原则吗? 修改参数并返回新实例(无需修改 关于参数

C++;修改参数和/或返回新实例的方法/函数的命名约定? 我想知道在编写这样的方法时,C++中是否有一个“被广泛接受的”命名约定: class Transform3d { public: // Apply rotation using a copy of toRotate and return it Vector3d ApplyRotation(const Vector3d& toRotate) const; // Apply rotation directly to toRotate void ApplyRotationInPlace(Vector3d& toRotate) const; // Apply rotation on this Transform3d instance (orientation quat) void ApplyRotation(const Vector3d& toRotate); //... // Apply transformation using a copy of toTransform and return it Transform3d ApplyTransform(const Vector3d& toTransform) const; // Apply transformation directly to toTransform void ApplyTransformInPlace(Vector3d& toTransform) const; private: Quaternion orientation; Vector3d position; }; P>是否有编写C++方法的准则,这些方法涉及参数修改和返回新实例(对参数没有修改)? < P>是的,你偶然发现了它。这是常量修饰符 < C++ >编写C++方法有一些指导原则吗? 修改参数并返回新实例(无需修改 关于参数,c++,naming-conventions,overloading,C++,Naming Conventions,Overloading,通过标记参数const,这正是您所保证的const确实是为开发人员准备的 在您的示例中,它已经存在于语言本身中。按值返回=(新对象)传入常量引用=(不可修改)-传入非常量引用=(可修改) 我建议不要装饰方法名,因为现在您的代码维护量增加了一倍,每次您决定更改函数的工作方式时,都必须在任何地方更改函数名。一些纯粹主义者可能会认为Transform3d应该是一个不可变的类:)从签名来看,很明显ApplyTransformInPlace修改toTransform,因为它接受非常量引用并返回void。额

通过标记参数
const
,这正是您所保证的
const
确实是为开发人员准备的

在您的示例中,它已经存在于语言本身中。按值返回=(新对象)传入常量引用=(不可修改)-传入非常量引用=(可修改)


我建议不要装饰方法名,因为现在您的代码维护量增加了一倍,每次您决定更改函数的工作方式时,都必须在任何地方更改函数名。

一些纯粹主义者可能会认为Transform3d应该是一个不可变的类:)从签名来看,很明显
ApplyTransformInPlace
修改
toTransform
,因为它接受非常量引用并返回
void
。额外的冗长只是杂乱无章。请从函数名中删除
Apply
,它不会添加任何内容<代码>旋转和
变换
将传达信息。