朋友在这里好主意? [英] Friend a good idea here?

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

问题描述

#include< vector>


class Bar;


class Foo

{

朋友班吧; / * 1 * /


受保护:

int myint;

Foo(){Foo :: Foo(0); }

Foo(int anum){myint = anum;}


public:

int GetMyInt(){return myint; }

};


班级酒吧

{

受保护:

std :: vector< Foo> vec;


public:

void AddAFoo(int anum){Foo aFoo(anum); vec.push_back(aFoo);}

};


int main(void)/ *仅用于show * /

{

返回0;

}


几个问题:


在/ * 1 * /的声明是正确的,但是我的实施接受了


朋友吧;


那天是合法的合法吗?如果没有,为什么我的

实现会接受它?


另一个问题是关于朋友的使用。

的想法是,只有Foo的朋友应该能够实例化它并且

修改其成员 - 这是一个有价值的目标,并且是朋友最好的方式

来完成它?


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

解决方案

Christopher Benson-Manica写道:

#include< vector>

班级酒吧;

班级Foo
{
朋友班吧; / * 1 * /

受保护:
int myint;
Foo(){Foo :: Foo(0);}


那是完全错误的。阅读调用构造函数任何地方

你可以找到它。如果打算共享一些代码,你需要

将它放在一个公共函数中并在两个构造函数中调用它。

这不是Java。


你应该写的


Foo():myint(0){}

Foo(int anum){myint = anum;}


首选表格是


Foo(int anum):myint(anum){}
public:
int GetMyInt(){return myint;}


在这里你可能想要的是正确性:


int GetMyInt()const {return myint;}

};

类吧
{
protected:
std :: vector< FOO> vec;

公开:
void AddAFoo(int anum){Foo aFoo(anum); vec.push_back(aFoo);}


你可以写


void AddAFoo(int anum){vec.push_back(anum); }


和用户定义的转换将处理它。一个临时的

将在没有明确声明的情况下创建。

};

int main(void)/ *仅用于show * /
{
返回0;
}

几个问题:

/ * 1 * /的声明是正确的,但我的实施接受了

朋友吧;

这一天是合法的合法吗?如果没有,为什么我的
实施会接受它?


可能是。如果我的记忆很好,那就不再了。

另一个问题是关于朋友在这里的使用情况。
的想法是,只有Foo的朋友应该能够实例化它并修改其成员 - 这是一个有价值的目标,并且是朋友最好的方式来完成它吗?



首先,如果只有朋友[和班级成员]必须能够实现Foo的
,那么你最好使构造函数_private _,

不受保护。否则我可以从Foo派生并实例化我想要的所有



其次,是的,它是工厂类的常见模式/>
授予某些类的扩展权限,这样只有他们才能创建该类的实例。


Victor


2004年7月8日星期四12:43:24 +0000(UTC),Christopher Benson-Manica

< at *** @ nospam.cyberspace。有机>写道:

#include< vector>

班级酒吧;

班级Foo
{
朋友班吧; / * 1 * /

受保护:
int myint;
Foo(){Foo :: Foo(0);}
Foo(int anum){myint = anum;}

公开:
int GetMyInt(){return myint;}
};

班级栏目
{
protected:
std :: vector< Foo> vec;

公开:
void AddAFoo(int anum){Foo aFoo(anum); vec.push_back(aFoo);}
};

int main(void)/ *仅用于show * /
{
返回0;
几个问题:

/ * 1 * /的声明是正确的,但我的实现接受了朋友Bar;

当天是否合法合法?如果没有,为什么我的
实施会接受它?


你的编译器是什么?也许是假设一个名字的功能。

另一个问题是关于朋友在这里的使用。
的想法是,只有Foo的朋友应该能够实例化它并修改其成员 - 这是一个有价值的目标,并且是朋友最好的方式来完成它吗?



我喜欢用朋友上班,而不是其他。它肯定不是必需的,但它有时会让事情变得更容易。对于

示例,当您需要无序初始化某些成员时,或者

提供无法(轻松)在成员中完成的默认值

初始化列表。


我确定有一个聪明的设计模式来解决这个问题,但是......

为什么要这么麻烦?只要你意识到朋友们倾向于打破OOP

范式和封装一般...但在工厂中,我认为它实际上会改善封装和封装隐藏

实施细节,只要工厂成为朋友的价格。


-

Bob Hairgrove
No**********@Home.com


Victor Bazarov< v。******** @ comacast.net>这样说:

这是完全错误的。阅读调用构造函数任何地方
你都可以找到它。


哦,你的意思是常见问题解答?多么古怪! (翻译:我很笨;()

和用户定义的转换会处理它。一个临时的
将在没有明确声明的情况下创建。


这里的主流做法似乎是宣布施工人员

明确......

可能是。如果我的记忆很好,那就是'不再是了。


我喜欢我的旧编译器!; P

首先,如果只有朋友[和班级成员]必须能够
实例化Foo,然后你最好使构造函数_private_,
不受保护。否则我可以从Foo派生并实例化我想要的所有。



对。我错过了 - 抱歉。


-

Christopher Benson-Manica |我*应该*知道我在说什么关于 - 如果我

ataru(at)cyberspace.org |不,我需要知道。欢迎火焰。


#include <vector>

class Bar;

class Foo
{
friend class Bar; /* 1 */

protected:
int myint;
Foo() {Foo::Foo(0);}
Foo( int anum ) {myint=anum;}

public:
int GetMyInt() {return myint;}
};

class Bar
{
protected:
std::vector<Foo> vec;

public:
void AddAFoo( int anum ) {Foo aFoo(anum); vec.push_back(aFoo);}
};

int main( void ) /* just for show */
{
return 0;
}

Couple of questions:

The declaration at /* 1 */ is correct, but my implementation accepts

friend Bar;

Was that syntatically legal back in the day? If not, why would my
implementation accept it?

The other question is about the use of friend in general here. The
idea is that only friends of Foo should be able to instantiate it and
modify its members - is that a worthy goal, and is friend the best way
to accomplish it?

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

解决方案

Christopher Benson-Manica wrote:

#include <vector>

class Bar;

class Foo
{
friend class Bar; /* 1 */

protected:
int myint;
Foo() {Foo::Foo(0);}
That''s totally wrong. Read about "calling constructors" anywhere
you can find it. If the intention is to share some code, you need
to put it in a common function and call it in both constructors.
This is not Java.

You are supposed to write

Foo() : myint(0) {}
Foo( int anum ) {myint=anum;}
Preferred form is

Foo(int anum) : myint(anum) {}

public:
int GetMyInt() {return myint;}
Const-correctness is something you might want here:

int GetMyInt() const {return myint;}
};

class Bar
{
protected:
std::vector<Foo> vec;

public:
void AddAFoo( int anum ) {Foo aFoo(anum); vec.push_back(aFoo);}
You could write

void AddAFoo(int anum) { vec.push_back(anum); }

and the user-defined conversion will take care of it. A temporary
will be created without an explicit declaration.
};

int main( void ) /* just for show */
{
return 0;
}

Couple of questions:

The declaration at /* 1 */ is correct, but my implementation accepts

friend Bar;

Was that syntatically legal back in the day? If not, why would my
implementation accept it?
It probably was. If my memory serves me well, it''s not any more.

The other question is about the use of friend in general here. The
idea is that only friends of Foo should be able to instantiate it and
modify its members - is that a worthy goal, and is friend the best way
to accomplish it?



First of all, if only friends [and the class members] must be able
to instantiate Foo, then you better make the constructor _private_,
not protected. Otherwise I can derive from Foo and instantiate all
I want.

Second, yes, it''s a common pattern for factory classes when they are
granted extended permissions to some class so that only they can
create an instance of that class.

Victor


On Thu, 8 Jul 2004 12:43:24 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:

#include <vector>

class Bar;

class Foo
{
friend class Bar; /* 1 */

protected:
int myint;
Foo() {Foo::Foo(0);}
Foo( int anum ) {myint=anum;}

public:
int GetMyInt() {return myint;}
};

class Bar
{
protected:
std::vector<Foo> vec;

public:
void AddAFoo( int anum ) {Foo aFoo(anum); vec.push_back(aFoo);}
};

int main( void ) /* just for show */
{
return 0;
}

Couple of questions:

The declaration at /* 1 */ is correct, but my implementation accepts

friend Bar;

Was that syntatically legal back in the day? If not, why would my
implementation accept it?
What is your compiler? Perhaps it is assuming a function by that name.
The other question is about the use of friend in general here. The
idea is that only friends of Foo should be able to instantiate it and
modify its members - is that a worthy goal, and is friend the best way
to accomplish it?



I like to use friends for factory classes, and not much else. It''s
certainly not necessary, but it makes some things easier at times. For
example, when you need to initialize certain members out of order, or
supply default values which cannot (easily) be done in the member
initialization list.

I''m sure there is a clever design pattern to get around this, but ...
why bother? As long as you realize that friends tend to break OOP
paradigms and encapsulation in general ... in the factory, though, I
tend to think it actually improves encapsulation and hiding of
implementation details, as long as only the factory gets to be a
friend.

--
Bob Hairgrove
No**********@Home.com


Victor Bazarov <v.********@comacast.net> spoke thus:

That''s totally wrong. Read about "calling constructors" anywhere
you can find it.
Oh, you mean like the FAQ? How quaint! (translation: I''m dumb ;()
and the user-defined conversion will take care of it. A temporary
will be created without an explicit declaration.
The prevailing practice here seems to be to declare constructors
explicit...
It probably was. If my memory serves me well, it''s not any more.
I love my old compiler! ;P
First of all, if only friends [and the class members] must be able
to instantiate Foo, then you better make the constructor _private_,
not protected. Otherwise I can derive from Foo and instantiate all
I want.



Right. I misspoke - sorry.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


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

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