Thread: namespaces and friends

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    470

    namespaces and friends

    Say you have two classes A and B that live in different namespaces. How would you make A's class be a friend of B's. The obvious solution does not seem to work.

    Code:
    namespace a {
          class b::B;
          class A {  friend class b::B; };
    }
    
    namespace b {
          class B {
                 
          };
    }
    I pretty much fixed the problem with the code that I was working with by redesigning class A so that the variables were in protected space. Basically something like this

    Code:
    namespace a {
           class A { /* allow vars to be accessed as protected */ };
    }
    
    namespace b {
           class B;
           class C : public A {  
                   friend class B;
           };
    
           class B  { 
                
           };
    }
    However, I was wondering if this was something that C++ does not allow.
    Last edited by okinrus; 03-26-2004 at 01:56 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Your forward declaration of b::B isn't right....
    You can have a forward decl. of a class that the compiler hasn't seen yet, but you can't have a forward decl. of a namespace that the compiler hasn't seen yet.
    Code:
    namespace b 
    {
        class B; // forward decl. of B in b
    }
    
    namespace a 
    {
        class A { friend class b::B; };
    }
    
    namespace b 
    {
        class B { };
    }
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. namespaces and static class members
    By ygfperson in forum C++ Programming
    Replies: 1
    Last Post: 07-06-2003, 08:55 PM