[英]C++: member function within structures
因此,我有一个结构接受四个不同参数(名称,艺术家,大小和添加日期)的条目,但是我有另一个结构本质上是该条目结构的库,并且我想在其中创建一个插入成员函数具有一个参数的库结构,该参数是要放入库中的条目。
在HEADER.h中
struct MusicEntry{
string name, artist, date_added;
long size;
MusicEntry() = default;
MusicEntry(string name_str, string artist_str, long size_int, string date_added_str) :
name(name_str), artist(artist_str), size(size_int), date_added(date_added_str) {};
MusicEntry to_string();
};
struct MusicLibrary{
MusicLibrary(string) {};
MusicLibrary to_string();
MusicEntry insert(); //not sure how this should be passed with MusicEntry
};
在FUNCTION.cpp中
MusicEntry MusicLibrary::insert(){
//some code
}
每首歌曲都有一个唯一的ID,这就是通过插入成员函数传递的基本要求。
我假设您希望MusicLibrary包含MusicEntry的所有实例,因此您应该研究诸如std :: vector之类的通用容器。
http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html#VECTOR
将MusicEntry传递到MusicLibrary中应该使用引用(&)或指针(*)完成。
MusicEntry* MusicLibrary::insert(const MusicEntry* myEntry){
//some code
}
要么
MusicEntry& MusicLibrary::insert(const MusicEntry& myEntry){
//some code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.