朋友与静态 [英] Friend vs. Static

查看:70
本文介绍了朋友与静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:朋友功能和静态成员功能在功能方面有何不同?我的意思是,在调用它们之前,它们都不一定需要一个

的对象来创建它,并且要么只有类的静态成员才能访问

(即假设没有

类的对象在范围内 - 既不是通过参数接收也不是通过本地

声明)。任何静态成员函数都是这样的:


//访问静态成员i

static void cls :: func(int funny){i = funny;}


将执行与此功能非常相似的功能,假设它

是cls的朋友:


//访问静态成员我

void func(int funny){cls :: i = funny;}


所以我想了解更多关于两种用途之间的差异。

我想到的差异:

静态可以隐藏在访问权限之后,而任何人都可以调用

朋友。

静态访问成员语法略显清晰:)

许多课程可以授予友谊到同一个功能,不可能

有静电。

友谊不是继承到派生类 - 静态是。

可能(我不确定)静态成员可能变成虚拟,
支持运行时多态性。


现在我只是从五分钟的思考中记下这些点,所以如果有人通过它看看它们是否正确,那么它将是好的。
如果有的可能是
添加到列表中。


和另一个问题......

我的代码中有许多函数被声明为静态,而

他们的目的确实是朋友 - 理由:他们需要被受保护的特权隐藏起来!这甚至发生在我的班级

甚至没有一个静态成员......对某人感到困惑

试图阅读我的代码!任何人都可以告诉我这是一个很好的方式

绕过这个? (我甚至希望有一天静态和朋友

会成为同义词... LOL)


Samee


Samee

Question: How do friend functions and static member functions differ in
terms of functionality? I mean, neither necessarily needs an object of
the class to be created before they are called and either has access
only to static members of the class (ie. assuming no object of the
class is in scope - neither by arguments recieved nor by local
declarations). Any static member function like this:

//accessing static member i
static void cls::func(int funny){i=funny;}

would perform functions very similar to this function, assuming it
is a friend of cls:

//accessing static member i
void func(int funny){cls::i=funny;}

So I''d like to know more about the differences between the two uses.
Differences that I have come to think of:
statics can be hidden behind access priveleges, while anyone may call a
friend.
Statics access members with a slightly cleaner syntax :)
Many classes may grant friendship to the same function, not possible
with statics.
Friendship is not inherited to derived classes - statics are.
And probably (I''m not sure though) static members may become virtual,
supporting run-time polymorphism.

Now I just jotted these points down from five-minutes'' thought, so it
would be nice if someone went through it to see if they are right, and
if some could be added to the list.

And another question ...
I''ve had numerous functions being declared static in my code while
their purpose really is being friends - reason: they needed to be
hidden by a protected privilege! This even happenned when my class
didn''t even have a static member ... quite bewildering for someone
trying to read my codes! Can anyone tell me what is a nice way of
getting around this? (I have even hoped that one day static and friend
would become synonymous ... LOL)

Samee

Samee

推荐答案

" Samee Zahur" < SA ********* @ gmail.com>在消息中写道

news:11 ********************** @ l41g2000cwc.googlegr oups.com
"Samee Zahur" <sa*********@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com
我想到的差异:
静态可以隐藏在访问权限之后,而任何人都可以称之为朋友。


不一定。 A类的私人成员函数可以成为朋友

的B类。

静态访问成员语法稍微清晰:)
许多课程可以授予友谊到同样的功能,不可能用静力学。


B,C,D ......类可以给A中的静态

函数赋予友谊,当然静态函数然后得到访问因为

它是朋友,而不是因为它是静态的。

友谊不是继承到派生类 - 静态是。


是的,但是如果你想在派生类中访问一个成员变量,那么在基类中定义的
a静态函数就不会是任何除非你在定义静态函数时预期这是预期的。相比之下,你可以在定义派生类之后定义友元函数(

当然,你也可以在派生类中定义一个新的静态函数)。

可能(我不确定)静态成员可能变成虚拟的,支持运行时多态性。
Differences that I have come to think of:
statics can be hidden behind access priveleges, while anyone may call
a friend.
Not necessarily. A private member function of class A can be made a friend
of class B.
Statics access members with a slightly cleaner syntax :)
Many classes may grant friendship to the same function, not possible
with statics.
It is possible for classes B, C, D... to grant friendship to a static
function in A, though of course the static function then gets access because
it is a friend, not because it is static.
Friendship is not inherited to derived classes - statics are.
Yes, but if you wanted to access a member variable in a derived class, then
a static function defined in the base class won''t be any use unless you
anticipated this in defining the static function. By contrast, you can
define the friend function after the derived class has been defined (of
course, you could also define a new static function in the derived class).
And probably (I''m not sure though) static members may become virtual,
supporting run-time polymorphism.




静态函数不能是虚拟的。由于它们没有绑定到任何类

对象,因此对象所属的继承层次结构中的类不能用于在不同的静态函数之间进行选择。

-

John Carson



Static functions cannot be virtual. Since they are not bound to any class
object, the class within an inheritance hierarchy to which an object belongs
cannot be used to select between different static functions.
--
John Carson


我认为基本点是任何功能都可以做了一个

的朋友---非会员功能,普通会员功能或静态会员

功能---并且可以成为任何愿意授予的朋友< br $>
友情。

-

John Carson

I think that the fundamental point is that any function at all can be made a
friend --- non-member functions, regular member functions or static member
functions --- and can be made a friend of any class willing to grant
friendship.
--
John Carson


你好


朋友类用于通过容器定义迭代器。你可以用静态会员功能做到这一点。


Nadina

Hi

Friend classes are used to define iterators through containers. IAnd
you can do that with a static member funct.

Nadina


这篇关于朋友与静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆