Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
745 views
in Technique[技术] by (71.8m points)

c++ - Why would a struct need a friend function?

I'm trying to build a program whose source I downloaded from the internet. When I try to compile it, I get the error message

friend declaration specifying a default argument must be the only declaration

Here is the offending code:

typedef int Var;
struct Lit {
    int     x;
    // Use this as a constructor:
    friend Lit mkLit(Var var, bool sign = false);
    bool operator == (Lit p) const { return x == p.x; }
    bool operator != (Lit p) const { return x != p.x; }
    bool operator <  (Lit p) const { return x < p.x;  } 
inline  Lit  mkLit(Var var, bool sign) { Lit p; p.x = var + var + (int)sign; return p; }

I've found several questions that address this issue, but the matter is rather abstruse, and I don't really understand the explanations. The answer to this question indicates that I can fix matters by moving the inline defintion of mkLit before the struct, removing the default argument from the friend function declaration, and moving it to the inline definition. Is this correct?

But more basically, I don't understand why a struct needs a friend function, since its members are public anyway. The accepted answer to this question gives an answer (that my ignorance prevents me from understanding) in terms of argument dependent lookup, but I can't see that it applies in this case.

Is there any drawback to just deleting the friend function declaration, and moving the default argument to the inline function definition? If so, can you give me a simple example?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

But more basically, I don't understand why a struct needs a friend function, since its members are public anyway.

This is a misunderstanding. There are no structs and classes in C++, but C++ only has classes that can be declared with one of the keywords struct or class. The only difference is the default access, ie the following two are identical (apart from the order of their members, which matters if you take their address):

struct foo : private bar {
    int x;
private:
    int y;
};

And the same with class:

class foo : bar {    
    int y;
public:    
    int x;
};

Using class or struct to declare a class is purely a matter of convention. Hence, your question translates to "Why would a class need a friend function?" and the answer is: To allow the friend to access private fields.

The question you linked is about defining the friend function inline vs just declaring it, ie

struct foo { 
    friend void foofriend() { /*put implementation here*/ }
};

vs

struct foo {
    friend void foofriend();
};

void foofriend() { /*put implementation here*/ }

This is indeed related to ADL (tbh I also could not explain it) and is kind of orthogonal to the question what friends are good for.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...