Using streams
-
Hello, I need help in converting these two function into one function (since basicaly it is the same code). Does anoyone have an idea ?
struct File_Writer
{
File_Writer(ofstream& output) : m_output(output) { ; }
void operator(MyStruct& ms)
{
output << ms.a << endl;
output << ms.b << endl;
}
ofstream& m_output; };struct Display_Writer
{
void operator(MyStruct& ms)
{
cout << " a: " << ms.a << endl;
cout << " b: " << ms.b << endl;
}
};Thanks, Berlus
-
Hello, I need help in converting these two function into one function (since basicaly it is the same code). Does anoyone have an idea ?
struct File_Writer
{
File_Writer(ofstream& output) : m_output(output) { ; }
void operator(MyStruct& ms)
{
output << ms.a << endl;
output << ms.b << endl;
}
ofstream& m_output; };struct Display_Writer
{
void operator(MyStruct& ms)
{
cout << " a: " << ms.a << endl;
cout << " b: " << ms.b << endl;
}
};Thanks, Berlus
Berlus wrote:
void operator(MyStruct& ms)
You first need to show code that actually compiles.
Berlus wrote:
I need help in converting these two function into one function (since basicaly it is the same code).
Since they are outputting to two unrelated streams, what exactly is it you think can be merged?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
Hello, I need help in converting these two function into one function (since basicaly it is the same code). Does anoyone have an idea ?
struct File_Writer
{
File_Writer(ofstream& output) : m_output(output) { ; }
void operator(MyStruct& ms)
{
output << ms.a << endl;
output << ms.b << endl;
}
ofstream& m_output; };struct Display_Writer
{
void operator(MyStruct& ms)
{
cout << " a: " << ms.a << endl;
cout << " b: " << ms.b << endl;
}
};Thanks, Berlus
Since all output stream classes inherit from ostream, you can do this:
using std;
struct MyStruct {
int a; // just an example
double b; // just an example
//...
};
// this implements the streaming operator for all classes derived from std::ostream
ostream& operator<<(ostream& output, MyStruct& ms) {
output << " a: " << ms.a << endl;
output << " b: " << ms.b << endl;
return output;
}int main () {
MyStruct ms;
ms.a = 3;
ms.b = 3.1415926;
ofstream myfile("test.txt");
myfile << ms; // writes to file
cout << ms; // writes to console
}[edit]fixed implementation[/edit]
modified on Thursday, May 26, 2011 9:31 AM
-
Since all output stream classes inherit from ostream, you can do this:
using std;
struct MyStruct {
int a; // just an example
double b; // just an example
//...
};
// this implements the streaming operator for all classes derived from std::ostream
ostream& operator<<(ostream& output, MyStruct& ms) {
output << " a: " << ms.a << endl;
output << " b: " << ms.b << endl;
return output;
}int main () {
MyStruct ms;
ms.a = 3;
ms.b = 3.1415926;
ofstream myfile("test.txt");
myfile << ms; // writes to file
cout << ms; // writes to console
}[edit]fixed implementation[/edit]
modified on Thursday, May 26, 2011 9:31 AM
Thanks for the quick response. Is there a way to insert the "serialization" code into the struct itself ? something like:
#include <iostream>
#include <fstream>
using namespace std;struct MyStruct
{
ostream& operator<<(ostream& output) {
output << " a: " << a << endl;
output << " b: " << b << endl;
return output;
}int a; double b;
};
int main(void)
{
MyStruct ms;
ms.a = 5;
ms.b = 4.2;cout << ms;
// Write to a file without changing methods in MyStruct
ofstream my_file("me.txt");
my_file << ms;return 0;
}I tried it, and got compilation errors. IntelliSense: no operator "<<" matches these operands I am trying to create some sort of a generic ToString. Thanks, Berlus
-
Thanks for the quick response. Is there a way to insert the "serialization" code into the struct itself ? something like:
#include <iostream>
#include <fstream>
using namespace std;struct MyStruct
{
ostream& operator<<(ostream& output) {
output << " a: " << a << endl;
output << " b: " << b << endl;
return output;
}int a; double b;
};
int main(void)
{
MyStruct ms;
ms.a = 5;
ms.b = 4.2;cout << ms;
// Write to a file without changing methods in MyStruct
ofstream my_file("me.txt");
my_file << ms;return 0;
}I tried it, and got compilation errors. IntelliSense: no operator "<<" matches these operands I am trying to create some sort of a generic ToString. Thanks, Berlus
Hehe, that's what I first did, and hence my edit. :) Unfortunately the streaming operators expect the type to be streamed as second parameter. If it were the first parameter, then your code would work, as in class methods the instance of the class itself is always passed as a hidden parameter. So, what you can do, is overriding the subtraction operator like in either of these ways:
struct MyStruct {
int a;
MyStruct(int value) : a(value) {} // conversion constructor, for implicit casting of int to MyStruct
MyStruct operator-(const MyStruct& op2) {
return (a-op2.a);
}
};or
struct MyStruct {
int a;
MyStruct(int value) : a(vlaue) {}
};
MyStruct operator-(const MyStruct& op1, const MyStruct& op2) {
return op1.a - op2.a;
}Both implementations are equivalent. The only difference is that the first variant skips the first parameter of the second variant, because the instance will be used in its stead.