Advertisement

Ifstream problems - Programming Games AI by Example

Started by October 10, 2011 08:58 PM
2 comments, last by 5dollarshake 13 years, 1 month ago
Hello, I am new here!

I was just working through the "Programming Games AI by Example" book, trying to understand how a file was being loaded using ifstream. After debugging for ages, I can't understand how the variable NumNodes is being set to the number on the first line of the .map file. It is like the ">>" operator is being overloaded or some other technique that I have never come across before. Anyone know what could be happening here please?

Any help would be greatly appreciated, cheers!
[sourcelang="cpp"]template <class node_type, class edge_type>
bool SparseGraph<node_type, edge_type>::Load(std::ifstream& stream)
{
Clear();

//get the number of nodes and read them in
int NumNodes, NumEdges;

stream >> NumNodes;

....
}[/source]
Sorry, just posted this three times! It was coming up with some crazy SQL error so I thought it hadn't posted it!
Advertisement

It is like the ">>" operator is being overloaded or some other technique that I have never come across before.

Yes, that is exactly what is happening.

The C++ standard library provides overloaded [font="Courier New"]std::operator>>(std::istream&, T)[/font] and [font="Courier New"]std::operator<<(std::ostream&, T)[/font] for various types T, and uses those overloaded operators to perform formatted input and output respectively. This use of overload bitshift operators as formatting functions first appeared about 1991. The bitshift operators were chosen because (a) they're used infrequently and (2) they have a very low (although not the lowest) operator precedence.

These operators are a part of the standard library that makes up an ISO-compliant C++ development environment. You should get familiar with them.

Stephen M. Webb
Professional Free Software Developer


[quote name='5dollarshake' timestamp='1318280302' post='4871213']
It is like the ">>" operator is being overloaded or some other technique that I have never come across before.

Yes, that is exactly what is happening.

The C++ standard library provides overloaded [font="Courier New"]std::operator>>(std::istream&, T)[/font] and [font="Courier New"]std::operator<<(std::ostream&, T)[/font] for various types T, and uses those overloaded operators to perform formatted input and output respectively. This use of overload bitshift operators as formatting functions first appeared about 1991. The bitshift operators were chosen because (a) they're used infrequently and (2) they have a very low (although not the lowest) operator precedence.

These operators are a part of the standard library that makes up an ISO-compliant C++ development environment. You should get familiar with them.
[/quote]

Ahh brilliant, thank you, I will look into it.

This topic is closed to new replies.

Advertisement