What are the differences between Friend and Static classes / members?

5

What are the differences between classes and member variables friend and static within the definition of a class, including its applications. I know only that members of the static class belong to the class and not to the object. Do members of the friend class belong to the object?

    
asked by anonymous 27.02.2016 / 22:20

1 answer

4

A friend member does not belong to an instance of the object. That is, it can not access by this . Access will occur through an explicitly declared parameter.

The friendly member only delegates access to private and secured members to a role or class. That is, the code just states that a particular function or class can access its "inner" members as if it were part of the class, even though it is outside.

So if you declare that a teste(MeuTipo objeto&) function as friend within the MeuTipo class, when this function is set elsewhere, the objeto parameter can access all members of the MeuTipo class, even the private and protected .

Obviously if the function does not get a parameter with the specific type, it does not make much sense.

In a way we can say that the member friend is static and will be set somewhere outside the class.

It recalls a purely virtual method, but the mechanism is completely different. The member definition will not be made by polymorphism or inheritance. On the contrary.

Remembering that there is a friendly class declaration too. This statement says that any friendly class can access members of this class. Obviously this can only occur with an explicit reference to this class.

    
27.02.2016 / 23:30