由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++: friend function
相关主题
子类的assignment operator 怎么访问父类的private memberAn object of A automatically converted to an object of B.
请教一个C++问题一个sql问题:怎样实现 (((a1*10)+a2)*10+a3)*10 ...
为什么java要用setter和getter关于 VC++ vitual, reload 和 derive的一个问题...
大家都用那跟手指敲 - + = [ ] ;Question about friend in C++
老年工程师转行遇到下马威请教C++问题
[合集] 又被羞辱了一把... (转载)c++ 设计问题求助
overload "++i"里的operator“++”,怎么declare?请教C#里property的意义
C++ namespace 弱问问一个简单的:setter 和getter有什么用处?
相关话题的讨论汇总
话题: friend话题: function话题: class话题: c++话题: member
进入Programming版参与讨论
1 (共1页)
n**d
发帖数: 9764
1
C++ is a hybrid object-oriented language, not a pure one, and friend was
added to get around practical problems that crop up.
Could anyone give an problem/example which can not be resolved without
friend function?
f******y
发帖数: 2971
2
I think it is just for convenience. You can always get around it, though
your code will become less readable.

【在 n**d 的大作中提到】
: C++ is a hybrid object-oriented language, not a pure one, and friend was
: added to get around practical problems that crop up.
: Could anyone give an problem/example which can not be resolved without
: friend function?

f*****Q
发帖数: 1912
3
or less efficient. C++ really added too many features, IMHO.
F**********r
发帖数: 237
4
hmm...if you really want to keep your private member function private...
unless declare friend class, what else can you do to by pass it?
K****n
发帖数: 5970
5
Add more interface functions?
发信人: FrLeafClover (4LeafClover), 信区: Programming
标 题: Re: C++: friend function
发信站: BBS 未名空间站 (Wed Aug 27 17:41:17 2008)
hmm...if you really want to keep your private member function private...
unless declare friend class, what else can you do to by pass it?
g*****g
发帖数: 34805
6
Just use a Getter, declare a public getYourPriveMember function

【在 F**********r 的大作中提到】
: hmm...if you really want to keep your private member function private...
: unless declare friend class, what else can you do to by pass it?

r*******y
发帖数: 290
7
then what's the point of keeping a private function or data member private?
in many cases, you only trust some non-member functions or classes

【在 g*****g 的大作中提到】
: Just use a Getter, declare a public getYourPriveMember function
F**********r
发帖数: 237
8
Let's say I have a private member function ChangeMyDataMember which operates
my private data using a certain algorithm. And in general I don't want to
expose this function to either derived class or public. However there is a
class that I would like to expose this member function to(which would be a
friend class)...I assume Friend is the only way out?

【在 g*****g 的大作中提到】
: Just use a Getter, declare a public getYourPriveMember function
g*****g
发帖数: 34805
9
Let's take a primitive instance variable for example.
A getter gives you read access to instance variable, not write
access. And when you need to write, a setter ensures the
variable is written in the right way. e.g. Another instance variable
has certain relationship with this variable, using a setter
can make sure that variable will be changed accordingly.
Using a public variable, however, you make it wide-open, have no
control of how it will be accessed/changed.
The essence of OOP is high cohe

【在 r*******y 的大作中提到】
: then what's the point of keeping a private function or data member private?
: in many cases, you only trust some non-member functions or classes

g*****g
发帖数: 34805
10
Well, usually the risk is not an "unfriendly" class
will call your precious "changeData" function, if only
one class should have this priviledge, then they have
high coupling. You should consider aggregate these two
classes, or use one class as the other's private member class
if there's a strong dependency.
In most cases, the risk of using public data member is data
integrity, you change some data but not the others, and the
instance becomes corrupted.
If A, B, C have no dependency at all, what

【在 F**********r 的大作中提到】
: Let's say I have a private member function ChangeMyDataMember which operates
: my private data using a certain algorithm. And in general I don't want to
: expose this function to either derived class or public. However there is a
: class that I would like to expose this member function to(which would be a
: friend class)...I assume Friend is the only way out?

相关主题
[合集] 又被羞辱了一把... (转载)An object of A automatically converted to an object of B.
overload "++i"里的operator“++”,怎么declare?一个sql问题:怎样实现 (((a1*10)+a2)*10+a3)*10 ...
C++ namespace 弱问关于 VC++ vitual, reload 和 derive的一个问题...
进入Programming版参与讨论
y***n
发帖数: 1594
11
这个解释不错。

operates
to
a

【在 F**********r 的大作中提到】
: Let's say I have a private member function ChangeMyDataMember which operates
: my private data using a certain algorithm. And in general I don't want to
: expose this function to either derived class or public. However there is a
: class that I would like to expose this member function to(which would be a
: friend class)...I assume Friend is the only way out?

r*******y
发帖数: 290
12
so goodbug you don't use friend?
how do you aggregate?

【在 g*****g 的大作中提到】
: Well, usually the risk is not an "unfriendly" class
: will call your precious "changeData" function, if only
: one class should have this priviledge, then they have
: high coupling. You should consider aggregate these two
: classes, or use one class as the other's private member class
: if there's a strong dependency.
: In most cases, the risk of using public data member is data
: integrity, you change some data but not the others, and the
: instance becomes corrupted.
: If A, B, C have no dependency at all, what

g*****g
发帖数: 34805
13
As I said, in case you find a strong requirement like that,
and you think these 2 classes should not be one.
you can make one class a private member of the other class.
More likely than not, when you need a friend class, you didn't
organize data in the right way, and you want to cut the corner.

【在 r*******y 的大作中提到】
: so goodbug you don't use friend?
: how do you aggregate?

r*******y
发帖数: 290
14
Let's consider the following example:
CPoint
{
private:
int x, y;
};
CPointCollection
{
public:
setAllPoints(int a, int b);
private:
std::vector m_points;
}
CPointCollection and CPoint have high coupling, to access x/y of CPoint
we either write a setter or make CPointCollection a friend of CPoint
I think the friend way is better.

【在 g*****g 的大作中提到】
: As I said, in case you find a strong requirement like that,
: and you think these 2 classes should not be one.
: you can make one class a private member of the other class.
: More likely than not, when you need a friend class, you didn't
: organize data in the right way, and you want to cut the corner.

g*****g
发帖数: 34805
15
If CPoint is not gonna used outside of CPointCollection,
obviously you can make CPoint an inner class of CPointCollection,
you can even make x,y public

【在 r*******y 的大作中提到】
: Let's consider the following example:
: CPoint
: {
: private:
: int x, y;
: };
: CPointCollection
: {
: public:
: setAllPoints(int a, int b);

a****l
发帖数: 68
16
operator overloading,
y******e
发帖数: 203
17
friend function is not necessarily needed.
for operator overloading, you can provide some getter functions, right?
I think friend function is only provided for convenience.
r*******y
发帖数: 290
18
take the following example:
class Base
{
private:
virtual f();
}
class Derived : public Base
{
public:
virtual f(); //Error, cannot override base
}
If you don't declare Derived as a friend of Base,
you will have compilation errors in many mordern compilers
friend is not just for convenience, a friend function is considered an
interface
of the class, you can just treat it as a public member function

【在 y******e 的大作中提到】
: friend function is not necessarily needed.
: for operator overloading, you can provide some getter functions, right?
: I think friend function is only provided for convenience.

g*****g
发帖数: 34805
19
I am no C++ guru, but this is certainly some C++ limitation,
not that friendly is justified.

【在 r*******y 的大作中提到】
: take the following example:
: class Base
: {
: private:
: virtual f();
: }
: class Derived : public Base
: {
: public:
: virtual f(); //Error, cannot override base

i******r
发帖数: 323
20
如果f()声明为protected
然后class derived : protected Base
可以吗?

【在 r*******y 的大作中提到】
: take the following example:
: class Base
: {
: private:
: virtual f();
: }
: class Derived : public Base
: {
: public:
: virtual f(); //Error, cannot override base

相关主题
Question about friend in C++请教C#里property的意义
请教C++问题问一个简单的:setter 和getter有什么用处?
c++ 设计问题求助goodbug vs neverlearn
进入Programming版参与讨论
Q**g
发帖数: 183
21
You sure about this? accessibility has nothing todo with polymophism. Try:
#include
class base {
public:
int callf() { return f(); }
private:
virtual int f() { return 0;}
};
class derived : public base {
public:
int f() { return 1;}
};
int main () {
base* b=new derived();
std::cout<< b->callf() << std::endl;
delete b;
}

【在 r*******y 的大作中提到】
: take the following example:
: class Base
: {
: private:
: virtual f();
: }
: class Derived : public Base
: {
: public:
: virtual f(); //Error, cannot override base

d******n
发帖数: 42
22
<< and >> operator must be implemented as friend non-member function.
Read effective C++ and you will see, it has more examples.
k**f
发帖数: 372
23

Not necessary friend functions. As long as the implementation can get
everything it need from the public interface.
Of course, I agree that they have to be non-member functions.

【在 d******n 的大作中提到】
: << and >> operator must be implemented as friend non-member function.
: Read effective C++ and you will see, it has more examples.

y******e
发帖数: 203
24
<< and >> operator must be implemented as friend non-member function.
If data can be available why << and >> operator must be implemented as
friend
1 (共1页)
进入Programming版参与讨论
相关主题
问一个简单的:setter 和getter有什么用处?老年工程师转行遇到下马威
goodbug vs neverlearn[合集] 又被羞辱了一把... (转载)
有没有根据 model 类自动生成 html form的工具?overload "++i"里的operator“++”,怎么declare?
Who can help explain setter and getter?C++ namespace 弱问
子类的assignment operator 怎么访问父类的private memberAn object of A automatically converted to an object of B.
请教一个C++问题一个sql问题:怎样实现 (((a1*10)+a2)*10+a3)*10 ...
为什么java要用setter和getter关于 VC++ vitual, reload 和 derive的一个问题...
大家都用那跟手指敲 - + = [ ] ;Question about friend in C++
相关话题的讨论汇总
话题: friend话题: function话题: class话题: c++话题: member