2011-12-07 3 views
2

내가 알고 싶은 그러한 방법 작성하지 않는 경우 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; 
}; 

매개 변수의 수정 및 새로운 인스턴스를 반환 다루는 ++ 방법 C를 작성하기위한 몇 가지 지침 거기를 매개 변수 수정)?

+1

일부 주안점은 ** Transform3d **는 변경 불가능한 클래스 여야한다고 주장 할 수도 있습니다. –

+1

서명에서 'ApplyTransformInPlace'는 비 const 참조를 취하고'void '를 반환하므로'toTransform '을 수정한다는 것은 명백합니다. 여분의 말씨는 혼란 스럽다. –

+2

함수 이름에서'Apply'를 삭제하십시오. 아무 것도 추가하지 않습니다. 'rotate'와'transform'은 메시지를 전달합니다. –

답변

3

예, 당신이 우연히 발견했습니다. const 수식어입니다.

는 ++ (수정없이 매개 변수에) 새로운 인스턴스를 매개 변수 변경을 다루는 반환 방법을 C를 작성하기위한 몇 가지 지침이 있습니까?

매개 변수를 const으로 표시하면 이것이 사용자의 보증 내용입니다. const은 개발자를위한 것입니다.

1

예를 들어 언어 자체에 이미 있습니다. 값에 의한 반환 = (새 객체) const ref = (변경 불가능) - 비 const ref = (수정 가능)에서 전달.

기능을 변경하기로 결정할 때마다 어디에서나 함수 이름을 변경해야하므로 메소드 이름을 장식하지 않아도됩니다. 코드 유지 관리가 두 배로 증가했기 때문에 좋습니다.