班上的朋友 [英] friends in a class

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

问题描述

我只是想知道班上的朋友是什么,我得到了一本关于c ++的书,但是它有足够深入的解释。任何人都想做一个

的例子,其中有一些关于朋友可以从中受益的笔记。

感谢

i am just wondering what is the friend in a class , ive got a book
about c++ but it dosent explain deep enough . anyone care to make a
example with a few notes on what the friend can benifit from.
thanks

推荐答案

an****@gmail.com 写道:
我只是想知道班上的朋友是什么,我得到了一本关于c ++的书,但它的解释非常深刻。任何人都可以用一些关于朋友可以获益的笔记来做一个
示例。
谢谢
i am just wondering what is the friend in a class , ive got a book
about c++ but it dosent explain deep enough . anyone care to make a
example with a few notes on what the friend can benifit from.
thanks




通过声明你正在授予的朋友对公众的访问权限,

受保护和私有部分的类。通常情况下,友情可以提供帮助

将您的大班级分成几个专门用于一个工作。


STL中迭代器的一个明显例子。为了使迭代器能够特别访问标准容器的内部,容器

必须将其迭代器声明为朋友。


更简单的例子是alarm_clock类,如下所示:


class alarm_clock_maintainer;


class alarm_clock

{

私人:

弹簧源;

变速箱gbox1;

变速箱gbox2;

// ...其他内部资料

public:

void run(void);

void set_alarm(time t );

无效重置(时间t);


朋友类alarm_clock_maintainer;

};

class alarm_clock_maintainer;

{

private:

maintainers_toolbox tools;

complex_stuff stuffs;

very_prof_knowledgebase知识;

//等等


公开:

alarm_clock&检查(闹钟和时钟);

alarm_clock& replace_parts(alarm_clock& clock);

alarm_clock&油(闹钟和时钟);

//等

};


现在请注意分割alarm_clock的界面你要分两个班级

立即使用更简单的alarm_clock,这不会带来所有工具箱,复杂的东西和知识的负担$ / b $ b $

保持一个时钟(因为大多数用户反正不需要那些。)但是你需要与alarm_clock_maintainer建立友谊,这需要访问

到春天闹钟中的变速箱和其他物品,其他用户通常不需要知道。


Ben



By declaring a friend you are granting access permission to the public,
protected and private part of the class. Usually, friendship can help
split your very large class into serveral dedicated on one job only.

An obvious example of the iterator in STL. In order for the iterators to
have special access to the standard containers'' internal, the containers
must declare its iterators as friends.

A simpler example is an alarm_clock class, like the following:

class alarm_clock_maintainer;

class alarm_clock
{
private:
spring source;
gearbox gbox1;
gearbox gbox2;
// ... other internal stuff
public:
void run(void);
void set_alarm(time t);
void reset(time t);

friend class alarm_clock_maintainer;
};

class alarm_clock_maintainer;
{
private:
maintainers_toolbox tools;
complicated_stuff stuffs;
very_prof_knowledgebase knowledge;
// etc

public:
alarm_clock& inspect(alarm_clock& clock);
alarm_clock& replace_parts(alarm_clock& clock);
alarm_clock& oil(alarm_clock& clock);
// etc
};

Now notice by spliting the alarm_clock''s interface into two classes you
immediately have simpler alarm_clock to work with, which does not come
with the burden of all the toolbox, complicated stuff and knowledge to
maintain a clock (because most user won''t need those anyways.) But you
need to grand friendship to alarm_clock_maintainer, which needs access
to the spring and gearboxes and other stuffs in the alarm_clock, which
other user normally won''t need to know.

Ben


谢谢ben

所以你总是需要在使用它之前对朋友类进行原型设计

我不确定这个


朋友类alarm_clock_maintainer;

};


这是在公共区域,所以不是它已经公开了,你做过吗?

并不意味着打字它在私人部分,所以它可以访问

公共?

thanks ben
so your always have to prototype the friend class , before you use it
and i am not sure about this

friend class alarm_clock_maintainer;
};

this was in the public section, so isnt it already public ,, did you
not mean to type it in the private section , so it can access the
public?


你我对上面的代码有点困惑。如果可以的话,我可以制作一个较小的版本,因为我倾向于每次做小的学习c ++,然后逐一了解它的作用

ye i am slightly confused with the above code ..would it be possible to
make a smaller version please as i tend to learn c++ from doing small
peices each time then understanding exactly what it does


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

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