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; Hey, I was wondering if you could make either a namespace or a function of a namespace a friend of ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Friend of namespace or of namespace function?

    Hey,
    I was wondering if you could make either a namespace or a function of a namespace a friend of a class.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,996
    You cannot make an entire namespace a friend. You can make any function you want a friend, however, inside a namespace or not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    You cannot make an entire namespace a friend. You can make any function you want a friend, however, inside a namespace or not.
    Ok, thanks. And is it better to put the namespace with said friend function of class before or after class definition?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,996
    The declaration of the function needs to be available prior to the friend declaration.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    The declaration of the function needs to be available prior to the friend declaration.
    Ahh, ok. So before then.
    I would have liked to have it after, but oh well...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,996
    You can define it afterwards, but you need the declaration before.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    You can define it afterwards, but you need the declaration before.
    How would I do that? Just write:

    Code:
    namespace NAMESPACE_NAME {
        //function1 prototype here
        //function2 prototype here
       //etc...
    }
    before the class definition, and then write
    Code:
    namespace NAMESPACE_NAME {
        //function1 definition here
        //function2 definition here
        //etc...
    }
    after the class definition?
    Last edited by Programmer_P; 03-13-2011 at 04:25 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,996
    Yes, the same as you would do with any function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    You simply need to have the prototype in the namespace.

    Code:
    namespace foo {
       class yourclass;
       void bar ( yourclass & obj );
    }
    class foo::yourclass
    {
       public:
       friend void bar ( yourclass & obj );
       // ...
    };
    
    void foo::bar ( yourclass & obj )
    {
       // ...
    }
    Is that what you mean by "after"?

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    You simply need to have the prototype in the namespace.

    Code:
    namespace foo {
       class yourclass;
       void bar ( yourclass & obj );
    }
    class foo::yourclass
    {
       public:
       friend void bar ( yourclass & obj );
       // ...
    };
    
    void foo::bar ( yourclass & obj )
    {
       // ...
    }
    Is that what you mean by "after"?
    No. My namespace that I'm writing does not include the class. The class is separate from the namespace. By "after", I meant all namespace functions' definitions would come after the class definition. But thanks for showing me I did not have to write namespace after the class definition. I just have to write FUNCTION_TYPE NAMESPACE_NAME::FUNCTION_NAME(...) { //... } for each function of the namespace.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    No. My namespace that I'm writing does not include the class.
    In that case, the class's forward declaration is simply placed outside the namespace before the prototype, which is in the namespace.

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    In that case, the class's forward declaration is simply placed outside the namespace before the prototype, which is in the namespace.
    Which prototype are we talking about? Sorry, the wording of your post confused me.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    OK, by definition, a prototype is a function signature such as

    void bar ( yourclass & obj )

    followed by a semicolon. You could also say "function prototype", but AFAIK, only functions have prototypes anyway.

    A forward declaration is for types. You simply name the type with one of the class-key keywords (which is in chapter 9 of the standard, if that helps) such as

    class yourclass;

    I thought I was being clear. I'm sorry for the confusion.

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    OK, by definition, a prototype is a function signature such as

    void bar ( yourclass & obj )

    followed by a semicolon. You could also say "function prototype", but AFAIK, only functions have prototypes anyway.

    A forward declaration is for types. You simply name the type with one of the class-key keywords (which is in chapter 9 of the standard, if that helps) such as

    class yourclass;

    I thought I was being clear. I'm sorry for the confusion.
    I know what a prototype is. I was just wondering which one you were talking about. I guess you were talking about the function prototypes of the namespace, but I don't see why a class forward declaration would be useful here. That's the part I was really confused about, but I said the wrong thing. Why would you put a class declaration followed by a namespace with function prototypes, followed by a class definition, followed by a namespace with function definitions (or just namespace function definitions)? Can't I just do a namespace with function prototypes, followed by a class definition, followed by a namespace with function definitions (or just namespace function definitions), and forget about using a class forward declaration?

    EDIT: Another thing...
    I decided to write just the namespace function definitions after the class definition (as opposed to the namespace with the function definitions approach), but one of my namespace functions requires a typedef'd type in one of its parameters. The typedef is local to the namespace. In that function's definition after the class's definition, do I need to specify the namespace name followed by double semicolons and the typedef name instead of just using the typedef name for the type of the function parameter?
    Last edited by Programmer_P; 03-13-2011 at 05:03 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    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.

Page 1 of 2 12 LastLast
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, 12:38 PM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

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