Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 简单C++;沮丧的韩元';我不适合简单的课程_C++ - Fatal编程技术网

C++ 简单C++;沮丧的韩元';我不适合简单的课程

C++ 简单C++;沮丧的韩元';我不适合简单的课程,c++,C++,我正在尝试创建一个通用的OpenFile,它表示一个打开的文件,我可以在我的实现中使用它自己的方式来保存一个打开的文件 #include <fstream> #include <memory> class OpenFile { }; class File : public OpenFile { public: std::unique_ptr<std::ofstream> file; }; int main() { OpenFile f

我正在尝试创建一个通用的
OpenFile
,它表示一个打开的文件,我可以在我的实现中使用它自己的方式来保存一个打开的文件

#include <fstream>
#include <memory>

class OpenFile {

};

class File : public OpenFile
{
public:
    std::unique_ptr<std::ofstream> file;
};


int main()
{
    OpenFile file;
    static_cast<File>(file).file;
    return 0;
}
?

更新:我尝试使用指针:

#include <fstream>
#include <memory>

class OpenFile {
public:
};

class File : public OpenFile
{
public:
    std::unique_ptr<std::ofstream> file;
};


int main()
{
    std::shared_ptr<OpenFile> file;
    auto f = std::dynamic_pointer_cast<File>(file);
    return 0;
}
#包括
#包括
类OpenFile{
公众:
};
类文件:公共OpenFile
{
公众:
std::唯一的_ptr文件;
};
int main()
{
std::共享的ptr文件;
自动f=std::动态指针转换(文件);
返回0;
}
但我明白了

/usr/include/c++/6/bits/shared_ptr.h: In instantiation of ‘std::shared_ptr<_Tp1> std::dynamic_pointer_cast(const std::shared_ptr<_Tp2>&) [with _Tp = File; _Tp1 = OpenFile]’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',26)">main.cpp:26:50</span>:   required from here
/usr/include/c++/6/bits/shared_ptr.h:458:22: error: cannot dynamic_cast ‘(& __r)->std::shared_ptr::.std::__shared_ptr<_Tp, _Lp>::get()’ (of type ‘class OpenFile*’) to type ‘class File*’ (source type is not polymorphic)
       if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))
/usr/include/c++/6/bits/shared_ptr.h:std::shared_ptr std::dynamic_pointer_cast(const std::shared_ptr&)的实例化[with _Tp=File;_Tp1=OpenFile]:
main.cpp:26:50:从这里开始需要
/usr/include/c++/6/bits/shared_ptr.h:458:22:错误:无法将“”动态转换为“”类文件“”(源类型不是多态的)
如果(\u-Tp*\u-p=dynamic\u-cast(\u-r.get()))

由于
文件
是从
OpenFile
派生出来的,所以可以说
文件
是一个
OpenFile
。这也意味着<代码> OpenFoos/COD>是<代码>文件<代码>?@ NANOLIVER ReNestMeNoCICA并不总是,但是C++下注是可能的。为什么它不起作用?如果您有一个指向
File
类型对象的
OpenFile
类型的指针/引用,则允许向下投射(使用
动态\u cast
)。这里的情况并非如此。唯一的对象类型为
OpenFile
。你认为你为什么需要这个cast?@umark我想有一个接口(.h文件),它保存一个OpenFile,但我想能够在我的类中启动这个file对象declaration@GuerlandoOCs“在我的类声明中启动这个文件对象”是什么意思?也许您应该在问题中添加一个您所设想的用例的完整示例。
#include <fstream>
#include <memory>

class OpenFile {
public:
};

class File : public OpenFile
{
public:
    std::unique_ptr<std::ofstream> file;
};


int main()
{
    std::shared_ptr<OpenFile> file;
    auto f = std::dynamic_pointer_cast<File>(file);
    return 0;
}
/usr/include/c++/6/bits/shared_ptr.h: In instantiation of ‘std::shared_ptr<_Tp1> std::dynamic_pointer_cast(const std::shared_ptr<_Tp2>&) [with _Tp = File; _Tp1 = OpenFile]’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',26)">main.cpp:26:50</span>:   required from here
/usr/include/c++/6/bits/shared_ptr.h:458:22: error: cannot dynamic_cast ‘(& __r)->std::shared_ptr::.std::__shared_ptr<_Tp, _Lp>::get()’ (of type ‘class OpenFile*’) to type ‘class File*’ (source type is not polymorphic)
       if (_Tp* __p = dynamic_cast<_Tp*>(__r.get()))