Search code examples
c++usingfriend

using keyword with class not allowed?


Hi I am trying to use a class nested in another one by using the usingkeyword. Is that possible with classes? I know that auto would do that and it would work with namespaces and base classes and functions.

Is my assumption for classes incorrect?

I am returned the following error

 error: ‘CTest’ is not a namespace

Below the source code

class CB {
  public: 
   void foo();
};

class CTest  { 
  public:   
    void foo();
  private:
    class CA {
      public:
        void foo() ;
    };        
  private:
    class CB b_;

    friend class CTest::CA;
    friend class CB;
};

void CTest::foo() { b_.foo();}
void CTest::CA::foo() { std::cout << "ciao" << std::endl;}
void CB::foo() { using CTest::CA; CA a; a.foo();}   

int main() {
  CTest obj;
  obj.foo();
}

Solution

  • C++11:

    void CB::foo() { using CA = CTest::CA; CA a; a.foo();}   
    

    C++98:

    void CB::foo() { typedef CTest::CA CA; CA a; a.foo();}