我正在查看一些代码,我想知道朋友操作员在这个类头文件中做了什么。
这是否需要说“当我使用此运算符并且两个输入都是CSegment3D类型时,我按照函数实现中定义的方式对它们进行操作”?
class CSegment3D : public CObject
{
public:
CSegment3D& operator*=(const double& factor);// multiply by a scalar factor & assign
CSegment3D& operator/=(const double& factor);// divide by a scalar factor & assign
CSegment3D& operator+=(const CSegment3D& other);// vector addition
CSegment3D& operator-=(const CSegment3D& other);// vector subtraction
friend CSegment3D GetMidpoint(const CSegment3D& ptA, const CSegment3D& ptB);
friend CSegment3D GetNormal( CSegment3D *pSeg1, CSegment3D *pSeg2, CSegment3D *pSeg3 ); // Return a point normal to the surface defines by the tree point passed in.
friend double operator*(const CSegment3D& vectorA, const CSegment3D& vectorB); // dot product
friend CSegment3D operator*(const double& factor, const CSegment3D& vectorA); // multiply by a scalar
friend CSegment3D operator/(const CSegment3D& vectorA, const double& factor); // divide by a scalar
friend CSegment3D operator+(const CSegment3D& vectorA, const CSegment3D& vectorB);// vector addition
friend CSegment3D operator-(const CSegment3D& vectorA, const CSegment3D& vectorB );// vector subtraction
friend CSegment3D operator^(const CSegment3D& vectorA, const CSegment3D& vectorB); // cross product
friend CSegment3D operator%(CSegment3D vectorA, const CSegment3D& vectorB); // projection of vectorB onto vectorA
};
答案 0 :(得分:4)
由于我总是想告诉我的学生,朋友就像一个女朋友:与朋友关键字的功能不是家庭,但仍然可以访问您最心爱的东西,在这种情况下,您的“私人”属性
答案 1 :(得分:1)
关键字friend用于允许函数访问类的私有成员,即使该函数不是该类的成员。友谊是在类定义中声明的,但函数仍然不是该类的成员,需要在其他地方声明和定义。
因此,例如,类定义中的friend CSegment3D GetMidpoint(const CSegment3D& ptA, const CSegment3D& ptB);
意味着函数GetMidpoint(const CSegment3D& ptA, const CSegment3D& ptB)
可以访问CSegment3D的私有成员,即使它不是该类的成员函数。
答案 2 :(得分:0)
这是另一个更技术性(和无聊)的样本/答案:
// Point2D.h
class Point2D() {
private:
float x,y;
public:
float GetX_Method() const;
// Next one is a function, so cant add const at end
friend float GetX_FriendFunction(const Point2D &p);
}
//Point2D.cpp
float Point2D::GetX_Method() const {
return x; // Since this method belongs to a point, C++ knows where to find x
}
float GetX_FriendFunction(const Point2D &p) {
// This is a friend function. Itdoesn't belong to the class
// but still can access private members of objects created from that
// class. All we need is the object itself (p in this case)
return p.x;
}
// This one isn't either method nor friend
float GetX_NoFriendFunction(const Point2D &p) {
// This will give an error since x is private
return p.x;
}
// main.cpp
main() {
Point2D p1;
// Prints P1 X. P1 is implicitly given by calling function
cout << p1.GetX_Method() << endl;
// Prints P1 X. P1 must be given as parameter
cout << GetX_FriendFunction(p1) << endl;
// This one throws error, since it can access private members of P1
cout << GetX_NoFriendFunction(p1) << endl;
}
// NOTES:
// * As you may guess, it's the class who says 'You're my friend', otherwise,
// anyone could access other's code and make bad things.
//
// * If you abuse friendship, you'll end up working as with C and structs
//
// * Typical use of friend are operators. For example, inside Point2D, you
// could write:
// This one will allow us to multiply one float to both X and Y
// and will be called this way:
//
// p1 = p1 * 5.0f; // Also p1 = p1.operator*(5.0f);
//
// Notice operator* only accepts float number. Point is implicitly given
Point2D operator*(float n);
// Now think about this one:
//
// p1 = 5.0f * p1;
//
// Mathematically it's the same, but following same rules than before,
// you must go to "float" class to add the operator* function, which is,
// of course, not possible :) so this is what you do
friend Point2D operator*(float left_operator, const Point2D& right_operator);
// That way, you use a friend function for do the same job
// Anyway, as said, you must avoid using friend as much as possible.
// This problem
//
// p1 = 5.0f * p1;
//
// Could have been solved without using friend using a plain C function
// like this:
Point2D operator*(float left_operator, const Point2D& right_operator)
{
// Gotta love how easy is to get my work done by others in C++
return right_operator*left_operator;
}