我正在使用我的代码,我遇到了这个问题。我想在单个向量中放入3个不同的类数据,因此我创建了基类向量。它看起来像这样:
std::vector <Video*> vid_list;
当我读取文件时,我使用其中一个派生类构造函数推送此向量中的数据:
vid_list.push_back(new Official_Music_Video(video_title, video_time,
Date(y,m,d), num_of_views));
所以我想在屏幕上显示这些数据,但我的基类只有
的方法std::string title;
double time;
Date date_of_release;
但不适用于int number_of_views
。我听说如果我想使用派生类getter方法我必须使用upcasting所以我试图做到这个
void views (Video& v) {
v.get_views();
}
然后在我的显示方法
void Official_Music_Video::display_info(std::vector <Video*>& vid_list){
Official_Music_Video ofc;
for (unsigned int i = 0; i < vid_list.size(); i++){
cout << vid_list[i]->get_title() << " " << vid_list[i]->get_time()
<< " " << vid_list[i]->write_year_to_file() << "-" <<
vid_list[i]->write_month_to_file() << "-" << vid_list[i]-
>write_day_to_file () << endl;
cout << "This video viewed " << vid_list[i].views(ofc) << " times"
<< endl;
}
}
但它没有用。你能帮我理解我犯错的地方和错误吗?
答案 0 :(得分:0)
如果所有三个派生类都有“number_of_views”方法,那么也将它放在基类中,问题就解决了。如果没有,那么你的代码是荒谬的。当你将“Unofficial_Music_Video”放入向量并在其上运行循环时,你会发生什么?
如果您确实需要在类中添加per-type行为,请考虑您实际想要完成的任务。您是否需要视频用户才能查询“number_of_views”,尽管所有子类型都不存在该字段?也许您只想访问人类可读的统计数据描述?如果是这种情况,最好在Video中添加“virtual std :: string StatsSummary()”方法,并让子类根据需要实现它。
答案 1 :(得分:0)
您可能希望将某些实现移动到基类左右,但这可以帮助您找到解决方案:
#include <vector>
#include <string>
#include <iostream>
class Date // for demo only
{
public:
Date(int y, int, int m, int d)
: y(y) , m(m), d(d) {}
int y;
int m;
int d;
};
class Video
{
public:
virtual std::string get_title() = 0;
virtual int get_time() = 0;
virtual int write_year_to_file() = 0;
virtual int write_month_to_file() = 0;
virtual int write_day_to_file () = 0;
virtual ~Video(){}
virtual void display_info()
{
std::cout << get_title()
<< " " << get_time()
<< " " << write_year_to_file()
<< "-" << write_month_to_file()
<< "-" << write_day_to_file ()
<< std::endl;
}
};
class VideoWithViewCount
: public Video
{
public:
virtual int views() = 0;
virtual void display_info()
{
Video::display_info();
std::cout << "This video viewed " << views() << " times" << std::endl;
}
};
class Official_Music_Video : public VideoWithViewCount
{
public:
Official_Music_Video(std::string _video_title, int _video_time, Date _date, int _Num_of_views)
: d(_date)
, time(_video_time)
, Num_of_views(_Num_of_views)
, video_title(_video_title)
{}
virtual int get_time() override {return time; };
virtual int write_year_to_file() override { return d.y; } // better call them get...
virtual int write_month_to_file() override {return d.m; }
virtual int write_day_to_file () override { return d.d; }
virtual int views() override {return Num_of_views; };
private:
Date d;
int time;
int Num_of_views;
std::string video_title;
};
class Other_Video : public Video
{
public:
Other_Video(std::string _video_title, int _video_time, Date _date)
: d(_date)
, time(_video_time)
, video_title(_video_title)
{}
virtual int get_time() override {return time; };
virtual int write_year_to_file() override { return d.y; }
virtual int write_month_to_file() override {return d.m; }
virtual int write_day_to_file () override { return d.d; }
private:
Date d;
int time;
std::string video_title;
};
int main(int,char**)
{
std::vector <Video*> vid_list;
// could also use unique_ptr or shared_ptr from #include <memory> in vector as follows:
// std::vector <unique_ptr<Video> > vid_list;
// std::vector <shared_ptr<Video> > vid_list;
// fill video vector
// vid_list.push_back(new Official_Music_Video(video_title, video_time, Date(y,m,d), num_of_views));
for (auto v : vid_list) // when using unique_ptr remember to use "const auto&" instead of "auto"
{
v->display_info();
}
return 0;
}