Why would a struct need a friend function?

后端 未结 3 1346
刺人心
刺人心 2020-12-21 19:01

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          


        
3条回答
  •  一整个雨季
    2020-12-21 19:55

    As many have mentioned before, a struct is the same as a class, except that its members are public by default. It can have private members.

    I'm guessing here, but I think the idea behind making this a friend factory function is that it the designer of the library is reserving the right to initialize any private members added in the future.

    To answer your questions:

    • Moving the default parameter to the inline function seems to work.
    • Moving the mkLit before the struct is a problem because it returns a Lit, and Lit hasn't been defined yet (since it now appears below mkLit).
    • Removing the friend declaration inside the struct will work because the struct has no private members -- for now. If you ever merge in a newer version of the original library that changes adds private members (or makes members private by default by changing the text struct to class), then it will stop working.

    (note: I pasted your code into http://godbolt.org and tried the various permutations, using different compilers)

提交回复
热议问题