Overload operator in subclass

前端 未结 2 1766
梦毁少年i
梦毁少年i 2021-01-07 12:22

tour and guided tour. Guided tour extends the tour class. I\'m overloading << and >> operators in the tour class.

My tour class looks like

#inc         


        
2条回答
  •  抹茶落季
    2021-01-07 12:38

    You're almost doing the right thing, but not quite. Let's take the output case first -- the input case works just the same.

    First, you should declare a

    virtual void write(std::ostream&) const;
    

    member function in your base class. The implementation might be something like:

    void Tour::write(std::ostream& os) const
    {
        os << "ID: " << id << std::endl;
        os << "Description: " << description << std::endl;
        // etc
    }
    

    which I assume is the sort of code you have currently in your operator<<(ostream&, Tour&). Then you need to overload this in your derived class -- perhaps with something like

    void GuidedTour::write(std::ostream& os) const
    {
        Tour::write(os); // Write out base Tour info first
        os << "Guide Name: " << guideName << std::endl;
        // etc
    }
    

    After that, you can declare a free (i.e. non-member) operator<< overload for Tours, which calls your write() member function, like

    std::ostream& operator<<(std::ostream& os, const Tour& tour)
    {
        tour.write(os);
        return os;
    }
    

    for example.

    Explanation: Forget the fact that your current operator<< is a friend; it has no bearing in this case. Instead, imagine you have two overloaded non-member functions called

    void do_something(Tour& t); // (a)
    void do_something(GuidedTour& gt); // (b)
    

    Since your tourListVector contains (I assume) Tour* pointers, if you were to loop through the vector and call do_something() on each element, the compiler would only be able to match function (a) above. That's because it has no way of knowing that some of the Tour* pointers might, for a given run of your programme, actually point to GuidedTour instances. In order to do run-time dispatching like this, you need to use virtual functions.

    Aside: (I know this is example code, but if you're new to C++ then it's worth pointing out just in case you weren't aware :-) )

    Because you're using Tour* pointers, you should define a virtual destructor for your base class. If you don't, the compiler won't know that it might need to destruct all the members of the GuidedTour class when you call delete on a Tour*. In fact, it's generally good practice to make your destructor virtual if your class includes any other virtual functions, just to save potential problems later.

    Also, please don't put using namespace std; in a header file :-)

提交回复
热议问题