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

Asked

Viewed 298 times

6

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

1 answer

4


A friendly member does not belong to an instance of the object, that is, can not access by this, the access will be through a parameter explicitly declared.

The friendly member only delegates access to private and protected members to a function or class, so the code only states that a specific function or class can access its "internal" members as if it were part of the class, even if it were outside.

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

Obviously if the function does not receive 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 else outside the class.

It sounds a little bit like a purely virtual method, but the mechanism is completely different. Member definition will not be made by polymorphism or inheritance, on the contrary.

Remember that there is the declaration of the friendly class as well. This statement says that the whole friendly class can access the members of this class. Obviously this can only occur by an explicit reference to this class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.