ほとんどの場合、例えばcout
のようなostream
オブジェクトにそのクラスを供給したいときに、friend
にすることでオーバーロードします。しかし、私にとってはそうではありません。<<を使用してクラスにファイルオブジェクトを供給するためにoperator <<を使用するにはどうすればよいですか?
std::fstream fileobj(".\example.file", std::ios::in | std::ios::binary);
AraHaan::hexstream hexstrm;
hexstrm << fileobj; //<-- itterates through the file object and hex arrays the data in it.
問題は、私はこれを行うことができるかわからないです。私は私がこのような何かをやらせることができますoperator<<
の過負荷を必要としています。次のようにhexstream
の現在のクラスのコードは次のとおりです。
#ifndef HEXSTREAM_DEFINED
#define HEXSTREAM_DEFINED
#include "basic_hexstream"
namespace AraHaan {
class hexstream: public AraHaan::basic_hexstream {
};
}
#endif
とbasic_hexstream
:
#ifndef BASIC_HEXSTREAM_DEFINED
#define BASIC_HEXSTREAM_DEFINED
#include <sstream>
#include <iomanip>
namespace AraHaan {
class basic_hexstream {
private:
std::stringstream base_hexstream;
bool data_cleared;
bool append0x, writehexseparater;
public:
void AddCharacter(int character) {
data_cleared = false;
if (append0x) {
if (writehexseparater) {
base_hexstream << "0x" << std::uppercase << std::setfill('0') <<
std::setw(2) << std::hex << static_cast<unsigned short>(character) << ", ";
} else {
base_hexstream << "0x" << std::uppercase << std::setfill('0') <<
std::setw(2) << std::hex << static_cast<unsigned short>(character);
}
} else {
if (writehexseparater) {
base_hexstream << std::uppercase << std::setfill('0') <<
std::setw(2) << std::hex << static_cast<unsigned short>(character) << ", ";
} else {
base_hexstream << std::uppercase << std::setfill('0') <<
std::setw(2) << std::hex << static_cast<unsigned short>(character);
}
}
}
void setoptions(bool append_0x, bool writehexseparator) {
append0x = append_0x;
writehexseparater = writehexseparator;
}
const std::string str() {
return base_hexstream.str();
}
void clear() {
data_cleared = true;
base_hexstream.clear();
base_hexstream.str("");
}
/*
TODO: make this work for ifstream, fstream, and FILE* file objects.
ex.
std::fstream fileobj(".\example.file", std::ios::in | std::ios::binary);
AraHaan::hexstream hexstrm;
hexstrm << fileobj; //< itterates through the file object and hex arrays the data in it.
*/
void operator<< (void* cool) {}
void operator<< (int character) {
// Note: Clearing the hexstream after every character is the user's responsibility
// if ran in a for loop that can dublicate the information that is if the data from
// the hexstream is obtained and added to a string after every itteration.
AddCharacter(character);
}
basic_hexstream() {}
~basic_hexstream() {
// clears the data just in case.
// This makes clearing this manually optional.
if(!data_cleared) {
clear();
}
}
};
}
#endif
しかし、ええ、私は、これは正しい動作させるためにbasic_hexstream
クラスで、このために適切にオペレータ< <を実装する必要があります何らかの形でファイルオブジェクトだけでなく、誰かがint
にキャストされたキャラクターを渡すときにも起こります。 上記の例のように< < <をhexstream
クラスにファイルオブジェクトとして入力するにはどうすればよいですか?また、必要な作業をすべて実行できますか?
"私はこのようなことをさせることができる演算子<<のオーバーロードが必要です:"あなたはそれをする必要はほとんどありません。名前を付けられた関数を使って、あなたが望むものを何でもすることができますが、これははっきりしません。他のiostream機能と統合したい場合は、演算子<<をオーバーロードする必要があります。これは、あなたのhexstreamクラスが関係していないためです。 –
私が望むのは、明示的に "AddCharacter"を呼び出さずにint文字をヘキサストリームに渡すための '' operator << ''だけでなく、毎回渡す前にファイルの反復を行うファイルオブジェクトを渡すためのバージョン文字は '' int''から '' AddCharacter''にキャストされます。基本的には長い話です。私は、人々が16進エディタのようなものに必要なファイル全体の内容を16進数で配列したいときに、すべてのファイルの反復処理を行うことができるようにしたいと考えています。だから基本的にはそれは可能な限り人々のコードをソートすることができますが、同じことを行うことができます – user2151283
何かがこの質問から欠落しているようです。それは実際の質問でしょう。ここに述べられている唯一のものはあなたが必要とするものです。それは問題ではありません。ここで書いた内容を編集し、具体的で簡潔な質問を追加する必要があります。 "私はこれを必要とし、私はそれが必要です"という長文の小説は、オフトピックですstackoverflow.com –