-1

Why is the operators << and >> called in base class if they are defined as 'friend' when calling << and >> in child class?

Code:

#include <iostream>
#include <memory>
#include <vector>

using namespace std;

class Baza
{
protected:
    int n;
public:
    Baza() { cout << "B\n"; }
    virtual ~Baza() { cout << "~B\n"; }

    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 << "D\n"; }
    ~Derivata() { cout << "~D\n"; }


};



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?

Rephrase: in base class (which is baza) are defined operators << and >> as friend. Derivata class inherits the 'baza' (base class), but operators are not inherited because they're friends. why are they called if i use >> and << in 'derivata' class?

13
  • 1
    Er, the derived class inherits base class stuff, like operators. This is expected behavior.
    – Dúthomhas
    Commented May 19 at 21:13
  • It has nothing to do with friend. Derivata is Baza and the operators with Baza& are chosen successfully.
    – 3CxEZiVlQ
    Commented May 19 at 21:16
  • sorry, i modified.
    – sticknycu
    Commented May 19 at 21:17
  • 2
    Simpler example showing the same phenomenon: void foo(const Baza&) {} int main() { Derivata d; foo(d); } Would you expect this version to not work?
    – JaMiT
    Commented May 19 at 21:17
  • 1
    You can remove friend ... and ensure friend is not a subject of your question.
    – 3CxEZiVlQ
    Commented May 19 at 21:21

1 Answer 1

1

The operator>> and operator<< you've defined are in no way part of the Baza class. The friend keyword doesn't do that; it just gives them permission to access Baza's private and protected members.

In that way, what you have is no different from this:

class Baza {};
class Derivata : public Baza {};

void some_function(Baza&) {}

int main() {
    Derivata d;
    some_function(d);
}

A reference to a base class can be bound to an object of a class derived from that base class, so a Baza& can be bound to a Derivata object. That allows you to pass objects of the derived class to functions that accept references to the base class.

Replacing some_function with an overloaded operator doesn't change anything about the semantics of how it's looked up or what objects can be passed to it. All it changes is the syntax used to call it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.