Reputation: 23
I am working with operator overloading and inheritance. I currently have something similar to the following code:
istream& operator >> (istream& in, derived& d)
{
/* Some code asking for input to populate the object's data members*/
cin >> d; /*Here, the operator called is this one, creating an infinite loop*/
}
The base class has an istream >> operator and when trying to invoke it, the operator actually calls itself, causing a loop. How can I access the base's operator from the derived's?
Upvotes: 1
Views: 338
Reputation: 172994
You need to convert it to the base class to call the operator>>
on base class, otherwise it would try to call itself and lead to infinite recursion. E.g.
istream& operator >> (istream& in, derived& d)
{
in >> static_cast<base&>(d);
return in;
}
PS: You should use in
instead of using cin
fixedly, and return in
in operator>>
.
Upvotes: 2