Pergunta

How to define friend for private classes?

#include <iostream>

class Base_t{
    private:
        struct Priv_t{
            friend std::ostream & operator<<(std::ostream &os, const Priv_t& obj);
        } p;
    friend std::ostream & operator<<(std::ostream &os, const Base_t& obj);
};

std::ostream & operator<<(std::ostream &os, const Base_t& obj) {
    return os << "base: " << obj.p;
}

std::ostream & operator<<(std::ostream &os, const Base_t::Priv_t& obj) {
    return os << "priv";
}

int main() {
    Base_t b;
    std::cout << b << std::endl;
}

error:

:!make t17 |& tee /tmp/vB5G5ID/54
g++     t17.cpp   -o t17
t17.cpp: In function 'std::ostream& operator<<(std::ostream&, const Base_t::Priv_t&)':
t17.cpp:5:16: error: 'struct Base_t::Priv_t' is private
         struct Priv_t{
                ^
t17.cpp:15:59: error: within this context
 std::ostream & operator<<(std::ostream &os, const Base_t::Priv_t& obj) {
                                                           ^
make: *** [t17] Error 1

shell returned 2

It works when I define friend directly in Priv_t

 friend std::ostream & operator<<(std::ostream &os, const Priv_t& obj) { return os << "priv"; }

How to do that outside class/struct definition?

Foi útil?

Solução

While Priv_t is a private declaration, you should move

friend std::ostream & operator<<(std::ostream &os, const Base_t::Priv_t& obj);

into the Base_t:

class Base_t
{
private:
  struct Priv_t
  {
  } p;
  friend std::ostream & operator<<(std::ostream &os, const Base_t& obj);
  friend std::ostream & operator<<(std::ostream &os, const Base_t::Priv_t& obj);
};

Live code.

Outras dicas

The overload for Priv_t will have to be a friend of Base_t as well as Priv_t.

Friendship must be explicit:

class Base_t
{
//...
private:
    //... 
    // Add:
    friend std::ostream & operator<<(std::ostream &os, 
                                               const Base_t::Priv_t& obj);
};
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top