简体   繁体   English

朋友班级如何访问嵌套班级的私有成员?

[英]How a friend class can access a private member of a nested class?

Consider the following example: 考虑以下示例:

class SIP{
    public:
        friend std::ostream& operator<<(std::ostream& os, const SIP& c);
    private:
        class BusStop;
        std::vector<BusStop*> mbusStops;
};

class SIP::BusStop{
    private:
        struct BusInfo;
        std::vector<BusInfo*> mbusStopTerminal;
};

struct SIP::BusStop::BusInfo{
    std::string from;
    std::string to;
};

std::ostream& operator<<(std::ostream &os, const SIP &c) {
    for (std::vector<SIP::BusStop*>::const_iterator it = c.mbusStops.begin(); 
         it != c.mbusStops.end(); it++){
        for (std::vector<SIP::BusStop::BusInfo*>::const_iterator it2 = mbusStopTerminal.begin(); 
             it2 != mbusStopTerminal.end(); it2++){
        }
    }
    return os;
}

It won't compile, because the BusInfo struct is private. 它不会编译,因为BusInfo结构是私有的。 Friend classes can't access private members of nested classes by default. 默认情况下,朋友类无法访问嵌套类的私有成员。 What should I do in that situation? 在那种情况下我该怎么办? Is there any workaround? 有什么解决方法吗?

You could add a stop-printing function to SIP : 您可以在SIP添加停止打印功能:

class SIP{
    public:
        friend std::ostream& operator<<(std::ostream& os, const SIP& c);
    private:
        void printStops(std::ostream& os);
        class BusStop;
        std::vector<BusStop*> mbusStops;
};

std::ostream& operator<<(std::ostream &os, const SIP &c) {
    c.printStops(os);
    return os;
}

or you could just add operators all the way down: 或者您可以一直添加运算符:

class SIP{
    public:
        friend std::ostream& operator<<(std::ostream& os, const SIP& c);
    private:
        class BusStop;
        std::vector<BusStop*> mbusStops;
};

class SIP::BusStop{
    private:
        friend std::ostream& operator<<(std::ostream& os, const BusStop& c);

        struct BusInfo;
        std::vector<BusInfo*> mbusStopTerminal;
};

struct SIP::BusStop::BusInfo{
    std::string from;
    std::string to;
};

std::ostream& operator<<(std::ostream &os, const SIP::BusStop::BusInfo &i)
{
   // Whatever
}

std::ostream& operator<<(std::ostream &os, const SIP::BusStop &c)
{
    for (std::vector<SIP::BusStop::BusInfo*>::const_iterator it = mbusStopTerminal.begin(); 
         it != mbusStopTerminal.end(); it++){
        os << **it;
    }   
}

std::ostream& operator<<(std::ostream &os, const SIP &c) {
    for (std::vector<SIP::BusStop*>::const_iterator it = c.mbusStops.begin(); 
         it != c.mbusStops.end(); it++){
        os << **it;
    }
    return os;
}

or any combination of approaches that suits your code. 或适合您代码的方法的任意组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在专用模板中访问嵌套朋友类的私有成员 - Access private member of nested friend class in specialized template 为什么朋友 class 成员可以通过公共继承的 class 的 object 访问其成为朋友的 class 的私人成员? - Why a friend class member can access a private member of the class it is being friend to through an object of publicly inherited class? 为什么这个朋友的功能无法访问该类的私有成员? - Why this friend function can't access a private member of the class? 为什么我的朋友类不能访问私有成员? - Why can't my friend class access a private member? 从朋友班访问私人成员 - access private member from a friend class 访问朋友类的私有成员 - Access friend class's private member friend operator &lt;&lt;和模板类成员的私有访问 - friend operator << and private access of template class member 如何使用好友类对象访问私有成员函数? - How to access private member function using friend class object? 在B类中声明为朋友的A类成员模板函数无法访问A类的私有成员(仅限Clang) - Class A member template function declared as friend in class B can't access private members of class A (Clang only) 无法访问在类中声明的私人成员,甚至无法访问声明为朋友的类 - Cannot access private member declared in class, even declared friend class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM