Friend of namespace or of namespace function?

This is a discussion on Friend of namespace or of namespace function? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by whiteflags I need to go back to my example. The compiler basically works in top down fashion, ...

  1. #16
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    I need to go back to my example.

    The compiler basically works in top down fashion, so we have:
    Code:
    namespace foo {
       class yourclass;
       void bar ( yourclass & obj );
    }
    class foo::yourclass
    {
       public:
       friend void bar ( yourclass & obj );
       // ...
    };
    
    void foo::bar ( yourclass & obj )
    {
       // ...
    }
    Now, comment out "class yourclass;" and compile: you get an error about how yourclass was not declared in this scope. Demystified, that means that the compiler to this point has no idea what the identifier yourclass is. This effects anything that depends on yourclass; IOW, because yourclass is not declared, the parameter of foo::bar() and by extension that bar() cannot be declared. So, yeah, you have to declare things in some capacity, before you attempt to use them.

    If we understand this, then we understand that "class yourclass;" should come before the namespace, because the declaration has to come before the prototype, which is in the namespace. It's the grammar of C++. It's not me.

    Also, part of the problem comes with the use of friend. I'm guessing, and don't really want to go into the details, that you are using friend for its intended purpose, such that any friend functions of yourclass would need to be friends, because they have yourclass parameter(s) and need access to private (or otherwise restricted) yourclass members. Most uses of friend can be made optional.
    Well, my namespace function which is going to be a friend of the class does not take a parameter to an object of the class. I made it a friend just so it could access a couple of private members of the class. It doesn't need to operate on a class object, and therefore, in my case, there is no real need for a class declaration, only a definition which follows the namespace with the function prototypes in it.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #17
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    I'm starting to lose track of what we are talking about, but if your function needs access to members of yourclass in any capacity it is necessary for the compiler to know what yourclass is, at that point, when it translates your source.

  3. #18
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    I'm starting to lose track of what we are talking about, but if your function needs access to members of yourclass in any capacity it is necessary for the compiler to know what yourclass is, at that point, when it translates your source.
    Right. And if I understand the "friend" concept right, you can declare an external function as a friend inside the class definition, and that allows the external function declared as a friend to do stuff with protected or private members of the class. I just assumed that the friend declaration prototype is supposed to mimic the function declaration prototype in the namespace, if the function declared as friend is a namespace function, i.e. so that the function declared as a friend inside the class needs to be declared first (which is what Elysia hinted at) outside the class (which I assumed to mean ABOVE the class definition).
    Last edited by Programmer_P; 03-13-2011 at 04:37 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #19
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    OK, well all of that has nothing to do with whether yourclass is declared or not. In my example, a forward declaration allows me to arrange the code the way I did. It is necessary.

    It's probably not necessary for your function because it doesn't take a parameter of type yourclass. I didn't know that until way later. When you originally said you don't see how a forward declaration was useful here, I took "here" to mean that you were talking about my example and not your own code. So I hope that explains my thinking.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. inline templated class method as standalone function in namespace
    By monikersupreme in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2008, 11:38 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21