在操作员中访问私有类<<在命名空间中 [英] Accessing private class in operator<< in namespace

查看:83
本文介绍了在操作员中访问私有类<<在命名空间中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CFoo类和一个私有内部类CBar.我想为CFoo实现流输出运算符,该运算符反过来在实现中使用CBar的流输出.当CFoo在公共命名空间中时,我可以使它工作,但是当我将其放置在新的命名空间(命名空间foobar)中时,操作员将无法再访问私有内部类.我怀疑这与运算符的完整签名有关,但是我无法找出指定朋友声明和实际运算符声明的正确方法,以便编译实现.谁能说出我可能会想念的东西? 请注意,如果流实现是在标头中内联完成的,它会编译,但是我讨厌不必要地公开这种实现!

I have a class CFoo with a private inner class CBar. I want to implement a stream ouput operator for CFoo, which in turn uses a stream output for CBar in it's implementation. I can get this working when CFoo is in the common namespace, but when i place it in a new namespace (namespace foobar), the operator can no longer access the private inner class. I suspect this has something to do with the full signature of the operator, but I can't figure out the correct way to specify the friend declaration and the actual operator declaration so the implementation compiles. Can anyone suggest what I might be missing? Note that it will compile if the stream implementation is done inline in the header, but I hate to expose implementation like this unnecessarily!

在foobar.h中(只需将usefoobarnamespace注释掉以测试非命名空间版本):

in foobar.h (just comment out the usefoobarnamespace to test the non-namespaced version):

#define usefoobarnamespace
#ifdef usefoobarnamespace
namespace foobar
{
#endif // usefoobarnamespace
    class CFoo
    {
    public:
        CFoo() {}
        ~CFoo();
        void AddBar();
    private:
        class CBar
        {
        public:
            CBar() {m_iVal = ++s_iVal;}
            int m_iVal;
            static int s_iVal;
        };

        std::vector<CBar*> m_aBars;

        friend std::ostream& operator<<(std::ostream& rcStream, CFoo& rcFoo);
        friend std::ostream& operator<<(std::ostream& rcStream, CFoo::CBar& rcBar);
    };
    std::ostream& operator<<(std::ostream& rcStream, CFoo& rcFoo);
    std::ostream& operator<<(std::ostream& rcStream, CFoo::CBar& rcBar);
#ifdef usefoobarnamespace
}
#endif // usefoobarnamespace

以及在foobar.cpp中:

and in foobar.cpp:

#ifdef usefoobarnamespace
using namespace foobar;
#endif // usefoobarnamespace

int CFoo::CBar::s_iVal = 0;


CFoo::~CFoo()
{
    std::vector<CBar*>::iterator barIter;
    for (barIter = m_aBars.begin(); barIter != m_aBars.end(); ++barIter)
    {
        delete (*barIter);
    }
}

void CFoo::AddBar()
{
    m_aBars.push_back(new CBar());
}


std::ostream& operator<<( std::ostream& rcStream, CFoo& rcFoo )
{
    rcStream<<"CFoo(";
    std::vector<CFoo::CBar*>::iterator barIter;
    for (barIter = rcFoo.m_aBars.begin(); barIter != rcFoo.m_aBars.end(); ++barIter)
    {
        rcStream<<(*barIter);   
    }
    return rcStream<<")";
}

std::ostream& operator<<( std::ostream& rcStream, CFoo::CBar& rcBar )
{
    return rcStream<<"CBar("<<rcBar.m_iVal<<")";
}

推荐答案

只需将.cpp文件中的代码放入命名空间:

Simply put the code in the .cpp file into the namespace:

namespace foobar {

// your existing code

}

这篇关于在操作员中访问私有类&lt;&lt;在命名空间中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆