Re: operator void* , stream extractor and built in types
From: Old Wolf (oldwolf_at_inspire.net.nz)
Date: 08/26/04
- Previous message: Tommy McDaniel: "Re: Garbage Collection - Stop Making Trash"
- In reply to: ma740988: "operator void* , stream extractor and built in types"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 25 Aug 2004 18:28:14 -0700
ma740988@pegasus.cc.ucf.edu (ma740988) wrote"
> The conversion function ios_base::operator void* () calls good() on
> its stream and returns the result.
>
> I searched Dikumware but came up short, so I'm assuming operator void*
> looks like
>
> operator void*()
> {
> return (good() ? void*(this) : 0 );
> }
In fact it can return any non-NULL value for the good case. I've
seen implementations that return (void *)1
> //// 2
> Assume input_file has the contents
> 25 130 "Sarah"
>
> class Test
> {
> public:
> Test() { } ;
> Test( int age_, int weight_, string name_)
> : Age(age_)
> , Weight(weight_)
> , Name(name_)
> { } ;
> ~Test() { };
>
> friend ostream& operator << ( ostream&, Test& );
> friend istream& operator >> ( istream&, Test& );
>
> void SaveAVec(Test &);
> vector<Test> Employ1;
>
> void WriteAllToScreen()
> {
[snipped to below]
> }
>
> private:
> int Age ;
> int Weight;
> string Name;
> };
>
> int main()
> {
> Test vec_test3;
> //Test vec_test3(25, 124, "Sarah");
> vec_test3.WriteAllToScreen();
> //cout << vec_test3;
> }
>
> Extractors - as i understand it - parse information expected by the
> destination object according to it's type. In this case the
> information (25 130 "Sarah") and the type (Test) are two unrelated
> entities, hence I'd expect the stream to fail.
What you are saying is, you would expect the function
operator<<(ostream&, Test&) to set the ostream to a fail state if
the information could not be parsed correctly.
What do you mean by "unrelated"?
25 130 "Sarah" relates to "Test" in exactly the same way
that 1.5 relates to "double" (ie. it can be parsed as it).
> The fact that that
> operator >> and subsequently the conversion operator got called for
> Employ1 object - a UDT - is somewhat of a mystery to me.
Me too, your code declares:
friend ostream& operator << ( ostream&, Test& );
friend istream& operator >> ( istream&, Test& );
but never implements them. You should get a linker error.
If you did implement these functions, then they would be called
whenever you went:
cout >> Employ1
because that is just another way of writing: operator>>(cout, Employ1)
> It further
> leads me to believe that the Employ1 looks like Test Employ1(25, 130,
> "Sarah").
It does
> Where have I gone wrong?
> Test Employ1;
Employ1 is of type "Test", so it must have a Name, Age and Weight.
Your code does not give any values to these in the default
constructor, so Employ1.Age, etc. have indeterminate values...
> while (in >> Employ1)
Now Employ1 will have values as assigned by the function
operator>>(istream&, Test&). If that function failed, then
Employ1 will have indeterminate values still.
> InVec.push_back(Employ1);
>
> This makes sense to me
> double f;
> cin >> f;
>
> assume f's input was 1.5. Now data and type match.
cin >> f would succeed if data and type matched, and fail if
they didn't match (eg. if f's input was "hello").
There is no difference between the 'double' example
and the 'Test' example. What is your problem exactly?
> An aside: I realize the 'correct way' would be along the lines of ...
>
> istream& operator >> ( istream& is, Test& rhs )
> {
> //cout << " istream& >>";
> is >> rhs.Age;
> is >> rhs.Weight;
> is >> rhs.Name;
>
> return is;
> }
This isn't really an "aside", you need to have these functions
present or your code won't even compile correctly.
- Previous message: Tommy McDaniel: "Re: Garbage Collection - Stop Making Trash"
- In reply to: ma740988: "operator void* , stream extractor and built in types"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]