我有一个电影基类和一些派生类(FunnyMovie,DramaMovie,ClassicMovie)。我需要将它们存储在电影的二进制搜索树中。每部电影都有不同的排序参数。首先,我正在尝试对FunnyMovies进行排序。当我比较两个FunnyMovie对象时,将调用FunnyMovie等于运算符函数。但是,当我比较两个包含有趣电影的Movie指针时,那么将调用Movie equal操作符函数,而不是FunnyMovie equal操作符函数。

如何使Movie指针调用其指向的(派生)对象的equal运算符函数,而不是基类Movie函数?

这是电影课:

#ifndef MOVIE_H
#define MOVIE_H
#include <sstream>
#include "RentableItem.h"
#include "Person.h"
using namespace std;

class Movie : public RentableItem
{
    friend ostream& operator<<(ostream &outStream, const Movie& movie);
public:
    Movie();
    Movie(int stock, string directorFirstName, string directorLastName, string title);
    ~Movie();

    string getTitle();
    Person& getDirector();
    char getMediaType();

    virtual bool operator==(const Movie& rhsMovie)const;
    virtual bool operator!=(const Movie& rhsMovie)const;
    virtual bool operator<(const Movie& rhsMovie) const;
    virtual bool operator>(const Movie& rhsMovie) const;
    virtual bool operator<=(const Movie& rhsMovie)const;
    virtual bool operator>=(const Movie& rhsMovie)const;

protected:
    Person* director;   // address
    string title;
    char mediaType;    // it is "D" for all movies as DVD.
};

#endif

这是电影相等运算符的实现:
#include "Movie.h"
using namespace std;

bool Movie::operator==(const Movie& rhsMovie)const
{
    return (*this->director == *rhsMovie.director && this->mediaType == rhsMovie.mediaType &&
        this->title == rhsMovie.title);
}

这是FunnyMovie类:
#ifndef FUNNYMOVIE_H
#define FUNNYMOVIE_H
#include "Movie.h"
using namespace std;

class FunnyMovie : public Movie
{
    friend ostream& operator<<(ostream &outStream, const FunnyMovie& movie);
public:
    FunnyMovie();
    FunnyMovie(int stock, string directorFirstName, string directorLastName,
        string title, int releaseYear);
    ~FunnyMovie();

    int getReleaseYear();
    bool operator==(const FunnyMovie& rhsMovie)const;
    bool operator!=(const FunnyMovie& rhsMovie)const;
    bool operator<(const FunnyMovie& rhsMovie)const;
    bool operator>(const FunnyMovie& rhsMovie)const;
    bool operator<=(const FunnyMovie& rhsMovie)const;
    bool operator>=(const FunnyMovie& rhsMovie)const;

private:
    int releaseYear;
};

#endif

这是equal运算符的FunnyMovie实现:
#include "FunnyMovie.h"
using namespace std;

FunnyMovie::FunnyMovie()
{
    releaseYear = 0;
}

FunnyMovie::FunnyMovie(int stock, string directorFirstName, string  directorLastName,
    string title, int releaseYear) : Movie(stock, directorFirstName, directorLastName,
    title)
{
    this->releaseYear = releaseYear;
}

FunnyMovie::~FunnyMovie()
{
}

bool FunnyMovie::operator==(const FunnyMovie& rhsMovie)const
{
    return (this->title == rhsMovie.title && this->releaseYear == rhsMovie.releaseYear);
}

我以为可能没有调用FunnyMovie方法,因为它们没有相同的参数,所以我改变了
bool FunnyMovie::operator==(const FunnyMovie& rhsMovie)const
// to:
bool FunnyMovie::operator==(const Movie& rhsMovie)const

但是,电影对象没有releaseYear,并且releaseYear对于FunnyMovie排序至关重要。

最佳答案

为了使虚拟函数能够在派生类上工作,它必须具有与基类相同的签名。因此,您的operator==()必须带有const Movie &参数。

我还没有尝试过,但是我想使您的比赛更加具体的方法是使用动态 Actor 表。如下所示。

// The "override" clause makes sure that the virtual function signature matches the one in the base class
bool FunnyMovie::operator==(const Movie &other) const override {
  bool result=false; // Presume different.

  FunnyMovie *fm_other=dynamic_cast<FunnyMovie *>(&other);
  if(fm_other){
    // Perform FunnyMovie comparison with ‘fm_other’ and set ‘result’.
  }
  else{
    // Perform generic Movie comparison with ‘other’ and set ‘result’.
  }

  return result;
}

关于c++ - 指向派生类对象的C++基类指针不调用派生相等运算符函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34232336/

10-09 01:38