Sign in to follow this  
5dollarshake

Ifstream problems - Programming Games AI by Example

Recommended Posts

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]

Share this post


Link to post
Share on other sites
[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.
[/quote]
Yes, that is exactly what is happening.

The C++ standard library provides overloaded [font="Courier New"]std::operator>>(std::istream&, [i]T[/i])[/font] and [font="Courier New"]std::operator<<(std::ostream&, [i]T[/i])[/font] for various types [i]T[/i], 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.

Share this post


Link to post
Share on other sites
[quote name='Bregma' timestamp='1318283472' post='4871232']
[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.
[/quote]
Yes, that is exactly what is happening.

The C++ standard library provides overloaded [font="Courier New"]std::operator>>(std::istream&, [i]T[/i])[/font] and [font="Courier New"]std::operator<<(std::ostream&, [i]T[/i])[/font] for various types [i]T[/i], 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this