Thread: Friend of namespace or of namespace function?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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.

  2. #2
    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.

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, 03: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