大規模なプロジェクトから簡略化した次のクラスの例があります。これは、ロガーのスコープを使用してデストラクタでログ エントリを終了するロギング フレームワークに基づいています。
以下のコードは、コンストラクターが暗黙的に削除された関数 ( edit: not true ) であるため、コンパイルされません。これは、std::ostringstream
オブジェクトと関係があるようです。を直接構築できるはずだと思うので、私はそれについて混乱しています。つまり、オブジェクトstd::ostringstream
を直接構築できるはずです。Container
#include <iostream>
#include <sstream>
class Container {
public:
std::ostringstream bufferStream;
public:
Container(); // constructor
~Container();
};
Container::Container() {
bufferStream << "Hello ";
}
Container::~Container() {
std::cout << bufferStream.str() << " [end]" << std::endl;
}
// === Main method ===
int main() {
Container().bufferStream << "world"; // works fine
{ // causes tons of compiler errors
Container cont = Container();
cont.bufferStream << "world!";
}
return 0;
}
「works fine」というラベルの付いた行がまさにそれを行っていることに注意してください。「世界」を出力するために直接アクセスできるContainer
new を含む匿名オブジェクトをインスタンス化するようです。std::ostringstream
それContainer
自体がメッセージの「Hello」部分を作成し、そのデストラクタがバッファをフラッシュします。
Container
オブジェクトに名前を付けて保存する 2 番目の部分が正しく実行されないのはなぜですか? これが私が得るエラーのサンプルです:
error.cpp: In function ‘int main()’:
error.cpp:28:36: error: use of deleted function ‘Container::Container(const Container&)’
Container cont = Container();
^
error.cpp:4:7: note: ‘Container::Container(const Container&)’ is implicitly deleted because the default definition would be ill-formed:
class Container {
^
error.cpp:4:7: error: use of deleted function ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’
In file included from error.cpp:2:0:
/usr/include/c++/4.8/sstream:387:11: note: ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
class basic_ostringstream : public basic_ostream<_CharT, _Traits>
... 等々。