<bdo id='8ogKQ'></bdo><ul id='8ogKQ'></ul>

      <i id='8ogKQ'><tr id='8ogKQ'><dt id='8ogKQ'><q id='8ogKQ'><span id='8ogKQ'><b id='8ogKQ'><form id='8ogKQ'><ins id='8ogKQ'></ins><ul id='8ogKQ'></ul><sub id='8ogKQ'></sub></form><legend id='8ogKQ'></legend><bdo id='8ogKQ'><pre id='8ogKQ'><center id='8ogKQ'></center></pre></bdo></b><th id='8ogKQ'></th></span></q></dt></tr></i><div id='8ogKQ'><tfoot id='8ogKQ'></tfoot><dl id='8ogKQ'><fieldset id='8ogKQ'></fieldset></dl></div>
      <legend id='8ogKQ'><style id='8ogKQ'><dir id='8ogKQ'><q id='8ogKQ'></q></dir></style></legend>
    1. <tfoot id='8ogKQ'></tfoot>

    2. <small id='8ogKQ'></small><noframes id='8ogKQ'>

    3. C++ 复制一个流对象

      C++ copy a stream object(C++ 复制一个流对象)

        1. <i id='u13od'><tr id='u13od'><dt id='u13od'><q id='u13od'><span id='u13od'><b id='u13od'><form id='u13od'><ins id='u13od'></ins><ul id='u13od'></ul><sub id='u13od'></sub></form><legend id='u13od'></legend><bdo id='u13od'><pre id='u13od'><center id='u13od'></center></pre></bdo></b><th id='u13od'></th></span></q></dt></tr></i><div id='u13od'><tfoot id='u13od'></tfoot><dl id='u13od'><fieldset id='u13od'></fieldset></dl></div>
            <bdo id='u13od'></bdo><ul id='u13od'></ul>
            <tfoot id='u13od'></tfoot>
            • <legend id='u13od'><style id='u13od'><dir id='u13od'><q id='u13od'></q></dir></style></legend>
                <tbody id='u13od'></tbody>

              <small id='u13od'></small><noframes id='u13od'>

              • 本文介绍了C++ 复制一个流对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我一直在试验 C++,但遇到了一个我不知道如何解决的问题.

                I've been experimenting with C++, and I've come across a problem that I don't know how to solve.

                基本上,我发现您无法复制流(请参阅为什么要复制不允许使用字符串流?),这也适用于包装"它们的对象.例如:

                Basically, I've discovered that you can't copy streams (see Why copying stringstream is not allowed?), and that also applies for objects that 'wrap' them. For example:

                • 我使用 stringstream 类型的数据成员创建了一个类.
                • 我创建了这个类的一个对象.
                • 我尝试复制对象,例如TestObj t1; TestObj t2; t1 = t2;"

                这会导致错误 C2249:

                This causes the error C2249:

                'std::basic_ios<_Elem,_Traits>::operator =' : 虚拟基类中声明的私有成员没有可访问的路径 'std::basic_ios<_Elem,_Traits>'

                'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'

                所以我的问题是:我如何(最好轻松)复制具有 *stream 类型数据成员的对象?

                So my question is: how can I (preferably easily) copy objects that have data members of type *stream?

                完整示例代码:

                #include <iostream>
                #include <string>
                #include <sstream>
                
                class TestStream
                {
                public:
                    std::stringstream str;
                };
                
                int main()
                {
                    TestStream test;
                    TestStream test2;
                    test = test2;
                
                    system("pause");
                    return 0;
                }
                

                提前致谢.

                更新

                感谢以下答案,我已经设法解决了这个问题.我所做的是声明一次流对象,然后使用包装对象(例如,TestStream)中的指针简单地引用它们.所有其他具有私有复制构造函数的对象也是如此.

                I've managed to solve this problem thanks the answers below. What I have done is declare the stream objects once and then simply reference them using pointers in the wrapper objects (eg, TestStream). The same goes for all other objects that have private copy constructors.

                推荐答案

                不允许复制流的原因是 复制流没有意义.如果你解释你想要做什么,那么肯定有办法做到.如果您想要可以复制的数据块,请使用字符串.但是流更像是一个连接而不是一个字符串.

                The reason you are not allowed to copy a stream is that it doesn't make sense to copy a stream. If you explain what it is that you are trying to do, there's certainly a way to do it. If you want a chunk of data you can copy, use a string. But a stream is more like a connection than a string.

                这篇关于C++ 复制一个流对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Is Type(::x); valid?(是类型(::x);有效的?)
                Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)

                <tfoot id='dKvEY'></tfoot>

              • <small id='dKvEY'></small><noframes id='dKvEY'>

              • <legend id='dKvEY'><style id='dKvEY'><dir id='dKvEY'><q id='dKvEY'></q></dir></style></legend>

                  • <bdo id='dKvEY'></bdo><ul id='dKvEY'></ul>

                        <tbody id='dKvEY'></tbody>
                        1. <i id='dKvEY'><tr id='dKvEY'><dt id='dKvEY'><q id='dKvEY'><span id='dKvEY'><b id='dKvEY'><form id='dKvEY'><ins id='dKvEY'></ins><ul id='dKvEY'></ul><sub id='dKvEY'></sub></form><legend id='dKvEY'></legend><bdo id='dKvEY'><pre id='dKvEY'><center id='dKvEY'></center></pre></bdo></b><th id='dKvEY'></th></span></q></dt></tr></i><div id='dKvEY'><tfoot id='dKvEY'></tfoot><dl id='dKvEY'><fieldset id='dKvEY'></fieldset></dl></div>