What is the friend element function in c?

Updated on technology 2024-02-25
10 answers
  1. Anonymous users2024-02-06

    The friend function should be described in a class in the form of:

    friend type name friend meta function name (form parameter table);

    The friend function is then defined outside the class, in the same format as a normal function, but the private members of the object can be directly accessed through the object as a parameter.

    The friend function is described as follows:

    1) The description of the friend function must be stated in the description of the class, starting with the keyword friend and followed by the function prototype of the friend function, and the description of the friend function can appear anywhere in the class, including in the private and public sections;

    2) Note that the friend function is not a member function of the class, so the implementation of the friend function is the same as that of the ordinary function, and it is not used in the implementation"::"Indicates which class it belongs to, and is only used by member functions"::"scope symbols;

    3) Friend functions cannot directly access the members of the class, only object members, 4) Friend functions can access the private members of objects, but ordinary functions cannot;

    5) When calling the friend function, you need to indicate the object to be accessed in the actual parameters

  2. Anonymous users2024-02-05

    The friend function is not part of the class, so both a and b are wrong.

    The friend function is declared in the class, and it is defined outside the class, and the friend keyword needs to be added when declaring and the parameter name can not be specified, but the friend keyword cannot be added when defining, so c is also wrong.

    I choose ABC.

  3. Anonymous users2024-02-04

    Hello!!!

    Meta functions are the same as ordinary functions, they are called directly, void increment(myclass& f) This is the definition of the function, increment(obj); This is the call of the function!!

  4. Anonymous users2024-02-03

    Because the friend function can directly modify all the data in the class.

  5. Anonymous users2024-02-02

    Youyuan has access to private public protection, and of course it can also be accessed.

  6. Anonymous users2024-02-01

    A friend function is a function that is not a member of the Naliang class but has access to all members of the or state class. The class grants it access to the Yuwon Special Cave Tour. Usually the same developer controls the friends and member functions of the class for both technical and non-technical reasons (otherwise you will have to ask the owners of the other parts of your class to agree when you want to update your class).

  7. Anonymous users2024-01-31

    1.Both friend functions and member functions of a class can access the class's private member variables or member functions, but they are called in different ways.

    2.The member function belongs to the class, so it is called by the pointer this. The friend function does not belong to the class, and of course there can be no this pointer, that is, this pointer cannot appear in the friend function.

    At the same time, the friend meta function is modified by the keyword friend.

    3.In other cases, the friend function is useful. This is especially useful when overloading some operators.

  8. Anonymous users2024-01-30

    This big brother is too careless.,public didn't add it.,setmember() is considered a private member.,Of course, it can't be compiled.。

    You can't let your guard down when you kill an insignificant little person

  9. Anonymous users2024-01-29

    1.A brief introduction to the friend function.

    Why use the friend function.

    Reduce system overhead and improve efficiency when implementing data sharing between classes. If a function in class A wants to access a member in class B (e.g., an implementation of a smart pointer class), then the function in class A is a friend function of class B. Specifically: for: to:

    Enables member functions of another class to directly access the class's private variables. That is, allowing an external class or function to access the class's private and protective variables, so that both classes share the same function.

    In fact, there are probably the following two situations where you need to use the friend function: (1) You need to use the friend element in some cases where the operator is overloaded. (2) When two classes want to share data.

    Advantages and disadvantages of using friend functions.

    Advantages: It can improve efficiency, and the expression is simple and clear.

    Disadvantages: The friend function breaks the encapsulation mechanism, try not to use the member function, and use the friend function unless it is a last resort.

    2.Use of friend functions.

    Arguments to the friend function:

    Because the friend function does not have this pointer, there are three cases for the parameter:

    To access a non-static member, you need an object as a parameter;

    If you want to access a static member or a global variable, you don't need an object as a parameter;

    If the object to be used as a parameter is a global object, the object is not required to be a parameter;

    The position of the friend element function.

    Because a friend function is an out-of-class function, its declaration can be placed in the private or public segment of the class without distinction.

    The call of the friend function.

    Friend functions can be called directly, without going through objects or pointers.

    Classification of friend functions:

    Ordinary functions, friend metafunctions.

    Purpose: To enable a normal function to access the friend of a class.

    Syntax: Declaration: friend + normal function declaration.

    Where it is implemented: Can be out of class or in class.

    Implementation: Same as normal function.

    Invocation: Similar to a normal function, it can be called directly.

  10. Anonymous users2024-01-28

    friend type name friend meta function name (form parameter table);

    The friend function is then defined outside the class, in the same format as a normal function, but the private members of the object can be directly accessed through the object as a parameter.

    1) The description of the friend function must be stated in the description of the class, starting with the keyword friend and followed by the function prototype of the friend function, and the description of the friend function can appear anywhere in the class, including in the private and public sections;

    2) Note that the friend function is not a member function of the class, so the implementation of the friend function is the same as that of the ordinary function, and it is not used in the implementation: indicate which class it belongs to, and only the member function is used: scope symbol;

    3) Friend functions cannot directly access the members of the class, only object members, 4) Friend functions can access the private members of objects, but ordinary functions cannot;

    5) When calling the friend function, you need to indicate the object to be accessed in the actual parameters, and 6) the friend relationship between classes cannot be inherited.

    7) A member function of a class can also be a friend of another class, but the class must be defined first.

Related questions
7 answers2024-02-25

The derivative of the tangent function is (secx) 2;

Derivatives are good local properties of functions. The derivative of a function at a point describes the rate of change of the function around that point. If both the independent variables and the values of the function are real, the derivative of the function at a point is the tangent slope of the curve represented by the function at that point. >>>More

8 answers2024-02-25

The basic oak representation of the quadratic function is y=ax +bx+c(a≠0). The quadratic function must be quadratic at its highest order, and the image of the quadratic function is a parabola whose axis of symmetry is parallel to or coincides with the y-axis. >>>More

6 answers2024-02-25

Understand the following rules: 1) Overloading an operator does not change the priority of the operator. >>>More

9 answers2024-02-25

They have common interests, hobbies, abilities, ideals, values, they appreciate each other, understand each other, tolerate each other, support each other, be honest, affectionate and righteous, and tolerate each other without exclusivity, this is true friendship! Wealth is not a lifelong friend, but a friend is a lifelong wealth! Friendship only sublimates, grows and develops after it is acquired! >>>More