Hey,
I was wondering if you could make either a namespace or a function of a namespace a friend of a class.
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.
How would I do that? Just write:
before the class definition, and then writeCode:namespace NAMESPACE_NAME { //function1 prototype here //function2 prototype here //etc... }
after the class definition?Code:namespace NAMESPACE_NAME { //function1 definition here //function2 definition here //etc... }
Last edited by Programmer_P; 03-13-2011 at 03:25 PM.
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
You simply need to have the prototype in the namespace.
Is that what you mean by "after"?Code:namespace foo { class yourclass; void bar ( yourclass & obj ); } class foo::yourclass { public: friend void bar ( yourclass & obj ); // ... }; void foo::bar ( yourclass & obj ) { // ... }
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.
In that case, the class's forward declaration is simply placed outside the namespace before the prototype, which is in the namespace.No. My namespace that I'm writing does not include the class.
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 04:03 PM.
I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.
I need to go back to my example.
The compiler basically works in top down fashion, so we have:
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.Code:namespace foo { class yourclass; void bar ( yourclass & obj ); } class foo::yourclass { public: friend void bar ( yourclass & obj ); // ... }; void foo::bar ( yourclass & obj ) { // ... }
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.