朋友和静态成员函数有什么类型的成员访问? [英] What kind of member access do friend and static member functions have?
问题描述
我想知道类成员访问 friend
函数和 static
成员函数具有类对象。具体来说,差异是什么,为什么要使用一个。这是我的理解,函数声明为 friend
一个类或 static
成员都可以访问任何实例的私有成员
我会举一个简单的例子来说明他们为什么像我这样相似。两个函数 operator
MyClass :: lessThan
似乎具有相同的访问权限,可以做同样的事情。
class MyClass {
friend bool operator<(const MyClass& left,const MyClass& right)
public:
MyClass(int pub,int priv):m_pub(pub),m_priv(priv){}
static bool lessThan(const MyClass& left,const MyClass& right) ;
int m_pub;
private:
int m_priv;
};
bool operator<(const MyClass& left,const MyClass& right){
return((left.m_pub< right.m_pub)&&(left.m_priv< ; right.m_priv));
}
bool MyClass :: lessThan(const MyClass& left,const MyClass& right){
return((left.m_pub< right.m_pub)& &(left.m_priv< right.m_priv));
}
int main(int argc,const char * argv []){
MyClass a(1,2),
b(3,4) b $ bc(-1,1);
cout<< a< b =< (a cout<< a lessThan b =< MyClass :: lessThan(a,b)<< endl;
cout<< a< c =< (a< c)< endl;
cout<< a lessThan c =< MyClass :: lessThan(a,c)<< endl;
}
很明显,这是一个简化的例子,但我想我的问题是两部分:
- 关于成员访问,朋友和静态函数之间的区别是什么?
- What are the differences between friend and static functions in regards to member access?
- What kinds of things should come into consideration when deciding which to use?
请记住,虽然原型朋友函数出现在类定义中时,朋友函数不是函数成员。类别的朋友功能在类别之外定义,但朋友功能可以访问非公开成员。有些人认为友谊腐蚀信息隐藏。有时朋友函数用于创建测试程序类。
当对象类应该只共享一个变量的副本时,使用静态数据成员。因此,只有一个副本对于所有类成员都足够,您必须使用静态数据成员来保存存储。
对于静态成员函数,您可以查看以下内容:何时使用静态成员函数?
I'm wondering what kind of class member access friend
functions and static
member functions have for class objects. Specifically, what the differences are and why to use one over the other. It's my understanding that functions declared as friend
of a class or as static
members both have access to private members of any instance of that class.
I'll give a quick example to show why they seem so similar to me. Both functions operator<
and MyClass::lessThan
seem to have the same access privileges and can do the exact same thing.
class MyClass {
friend bool operator<(const MyClass &left, const MyClass &right);
public:
MyClass(int pub, int priv) : m_pub(pub), m_priv(priv) {}
static bool lessThan(const MyClass &left, const MyClass &right);
int m_pub;
private:
int m_priv;
};
bool operator<(const MyClass &left, const MyClass &right) {
return ( (left.m_pub < right.m_pub) && (left.m_priv < right.m_priv) );
}
bool MyClass::lessThan(const MyClass &left, const MyClass &right) {
return ( (left.m_pub < right.m_pub) && (left.m_priv < right.m_priv) );
}
int main(int argc, const char* argv[]) {
MyClass a(1, 2),
b(3, 4),
c(-1, 1);
cout << "a < b = " << (a<b) << endl;
cout << "a lessThan b = " << MyClass::lessThan(a,b) << endl;
cout << "a < c = " << (a<c) << endl;
cout << "a lessThan c = " << MyClass::lessThan(a,c) << endl;
}
Clearly this is a simplified example, but I suppose my question is two-part:
Remember that although prototypes for friend functions appear in a class definition, friend functions are not function members. A friend function of a class is defined away from the class, but a friend function has got access to non public members. Some people think "friendship" corrupts information hiding. Sometimes friend functions are used to make tester classes.
Static data members are used when an object class should to share an only one copy of a variable. So, you must use static data members for saving storage when just one copy is enough for all class members.
For static member functions you can look the following: When to use static member function?
这篇关于朋友和静态成员函数有什么类型的成员访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!