gpt4 book ai didi

c++ - c++调用基类的ostream友元函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:15:30 24 4
gpt4 key购买 nike

所以,我有两个类:

class Base {
private:
int number;
public:
friend ostream & operator<<(ostream & output, const Base &n);
}

ostream & operator<<(ostream & output, const Base &n) {
output<<n.a<<endl;
return output;
}

class Child : Base {
private:
int second;
public:
friend ostream & operator<<(ostream & output, const Child &n);

}

ostream & output<<(ostream & output, const Child &n) {
output<<n.second<<Base:: ????<<endl;
return output;
}

我的问题是,如何从子类中调用基类的友元函数输出其内容:

output<<n.second<<Base:: ????<<endl

提前致谢:)

最佳答案

output<<n.second<<static_cast<const Base&>(n)<<endl;

关于c++ - c++调用基类的ostream友元函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10367271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com