Why is the operators << and >> called in base class if they are defined as ‘friend’ when calling << and >> in parent class?
Code:
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
class Baza
{
protected:
int n;
public:
Baza() { cout << "Bn"; }
virtual ~Baza() { cout << "~Bn"; }
friend istream& operator>>(istream& in, Baza& b);
friend ostream& operator<<(ostream& out, const Baza& b);
};
istream& operator>>(istream& in, Baza& b)
{
cout<<"op >>"<<endl;
return in;
}
ostream& operator<<(ostream& out, const Baza& b)
{
cout<<"op<<"<<endl;
return out;
}
class Derivata : public Baza
{
public:
Derivata() { cout << "Dn"; }
~Derivata() { cout << "~Dn"; }
};
int main()
{
vector<Derivata> v;
v.push_back(new Derivata);
// works
Derivata d;
cin>>d;
cout<<d;
return 0;
}
Is c++ compiler creating a operators that calls the base class method’s operator by default when compiling?