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?
|
|
|
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 | |
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
|
|
|
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 |