A Friend function can be avoided if get()(to read) and set()(to write) member functions(public) are provided on (private) data members of a class.
Is it True or False.
Can anyone help me clearing this doubt with an appropriate reason?
The friend keyword allows you to specify functions or other classes which will be able to access all member (public, protected or private) of the class in which the friend keyword is used. Thus one way to avoid using it would be to provide getters and setters. But this is not always a good approach because all your members will become 'visible' to everybody, which is not always what you want.
A friend function basically gives access to the internal data members of a class to an external function.
This can definitely be achieved using getters and setters.
But in reality friend functions are not used for this purpose.
For example, if you look at the CStringT class, the overloaded + operator is declared as a friend function. This is so that the + operator can be used to concatenate two string in the following 2 ways
If you do weird things in the = operator (re-)definition, for instance, i.e. if you write it (implicitely) assuming it will be never used to do self-assignment. Have a look at the following code
(please note, it is silly, written just to spot the point; moreover no check is done on memory allocation, for brevity)
Nope, anyway it's a good candidate to be inserted into the WPF (Worst Practice Foundation).
led mike wrote:
Or is this question about something entirely different than what I have been able to interpret?
led mike wrote:
And why would someone do that? Isn't that considered Best Practice? Or is this question about something entirely different than what I have been able to interpret?
It seems to me that it is about this subject[^]
Nope, you understand it correctly and, damn... If you recalled that page before my post I hadn't to put the weird code therein.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
Thanx for the adjective used for me(Worst Practice Foundation user).
Any way, my question was what are the problems associated with self assignment of an object to itself?
Ppl gave me all the comments and on my programming as well rather to answer my question, but yes, i got the answer, please refer the following link:
Refer: SECTION : Assignment operators(in the link above)