如何访问多个继承类中的受保护成员
How to access protected members in a multiple inherited class?
我想在派生类的构造函数中使用受保护的成员,但我无法使用它。我有一个课程,试图将输出重定向到标准或其他流。这是我的代码。redirection.h
#include <iostream>
#include <fstream>
class Redirection {
public:
Redirection(std::ostream &stream)
:outStream(stream)
{
};
Redirection()
:Redirection(std::cout)
{
};
protected:
std::ostream &outStream;
};
衍生.h
#include "Redirection.h"
class Derived : public Base, public Redirection
{
public:
Derived();
Derived(std::ostream& stream);
~Derived();
};
dedived.cpp
#include "Derived.h"
Derived::Derived()
:Derived(std::cout)
{
}
Derived::Derived(std::ostream& stream)
:Base(),
oustream(stream)
{
}
当我尝试构建时,我会收到以下错误:
error: class 'Derived' does not have any field named 'outStream'
,如果我这样修改:
Derived::Derived(std::ostream& stream)
:Base()
{
oustream = stream;
}
我有以下错误:
error: 'std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator=(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]' is protected
basic_ostream& operator=(const basic_ostream&) = delete;
error: within this context: mOutStream = stream;
由于多个继承,我会遇到这些错误?还是您知道如何解决?
作为评论中的状态,使用以下内容:
Derived::Derived(std::ostream& stream) : Base(), Redirection(stream) {}
相关文章:
- 为什么我可以通过SubBase类的公共继承方法打印出基类的私人继承成员
- 派生类调用使用非继承成员的继承函数
- 如果存在具有不同参数的继承成员,为什么对 C++ 结构函数的调用不明确?
- C :原始成员与继承成员的优先级
- 更改继承成员的地址
- C 是否可以不继承成员
- 使用模板的继承阴影继承成员
- 继承成员功能的模板专业化
- 不允许继承成员,为什么
- 派生模板类和继承成员变量的语法谜题
- 继承成员地址的C++标准
- 多级继承成员访问
- 访问专用模板的私有/继承成员变量
- 基模板继承成员,减少所需的类型
- 继承成员函数指针
- 模板专用化类不继承成员
- C++14正在初始化类构造函数初始化列表中的继承成员
- 具有专门初始化的继承成员变量
- 模板继承成员字段
- 访问数据成员的继承成员函数