朋友 class 如何访问嵌套 class 的私有成员?
How a friend class can access a private member of a nested class?
考虑以下示例:
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;
}
它不会编译,因为 BusInfo 结构是私有的。默认情况下,友元类无法访问嵌套类的私有成员。在这种情况下我该怎么办?有什么解决方法吗?
您可以在 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;
}
或者您可以直接添加运算符:
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;
}
或适合您的代码的任何方法组合。
考虑以下示例:
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;
}
它不会编译,因为 BusInfo 结构是私有的。默认情况下,友元类无法访问嵌套类的私有成员。在这种情况下我该怎么办?有什么解决方法吗?
您可以在 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;
}
或者您可以直接添加运算符:
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;
}
或适合您的代码的任何方法组合。