Friend Declaration in C ++

In Thinking in C ++ by Bruce eckel there is an example given regarding friend functions as

// Declaration (incomplete type specification):
struct X;
struct Y {
void f(X*);
};
struct X { // Definition
private:
int i;
public:
friend void Y::f(X*); // Struct member friend
};
void Y::f(X* x) {
x->i = 47;
}

Now he explained this:

Note that Y :: f (X *) takes the address of an X object. This is important because the compiler always knows how to pass an address that has a fixed size regardless of whether the object is passed, even if it does not have full information about the type size. However, if you try to pass the whole object, the compiler should see the definition of the entire X structure, know the size and how to pass it before it allows you to declare a function such as Y :: g (X).

But when I tried

void f(X);  

like an ad in structure Y, it shows no error. Please explain why?

+3
3

.

:

struct A;
struct B {
    void f(A);   // declaration, fine
    void g(A) {} // error
    A a;         // error
};
+1

X , X , X .   , covariant ( -), .

+1

X.

, struct X , , - X, , X !

f, , , X *.

On a note on the site, it will be very important for you to increase your reception speed so that you receive answers to your future questions and are important for my ego if I received the correct answer :)

0
source

Source: https://habr.com/ru/post/1744028/


All Articles