C++:结构内的成员功能
C++: member function within structures
所以我有一个结构,它接受四个不同参数(名称、艺术家、大小和添加日期)的条目,但是我还有另一个结构,它本质上是条目结构的库,我想在库结构中创建一个插入成员函数,该函数接受一个参数,该参数是要放置在库中的条目。
在标题中
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
};
在功能中
.cppMusicEntry MusicLibrary::insert(){
//some code
}
每首歌曲都提供了一个唯一的ID,这就是通过插入成员函数传递
我假设您希望 MusicLibrary 包含 MusicEntry 的所有实例,因此您应该查看通用容器,例如 std::vector。
http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html#VECTOR
将 MusicEntry 传递到音乐库中应使用引用 (&) 或指针 (*) 来完成。
MusicEntry* MusicLibrary::insert(const MusicEntry* myEntry){
//some code
}
或
MusicEntry& MusicLibrary::insert(const MusicEntry& myEntry){
//some code
}
相关文章:
- 类模板的成员功能的定义在单独的TU中完全专业化
- 升压信号2将插槽传递到成员功能以断开连接
- 多次实现成员功能
- 私有成员数据不可用于公共成员功能
- 如何避免大多数成员功能相同的代码重复
- C 成员功能
- =删除用户定义的成员功能,除了构造函数,分配运算符C 11
- C 朋友课程和朋友成员功能
- C 函数调用包装器包含类成员功能作为模板参数
- 创建类成员功能
- 成员功能C 的不同实现
- 返回类型不可知模板类成员功能
- C 11螺纹 - 非静态成员功能的使用无效 - 工作GCC 5.1.0损坏的GCC 7.3.1
- 超载类成员功能标记为const
- C 功能指针和成员功能指针
- 将非静态成员函数C 的使用无效,而将成员功能作为函数变量
- 我可以使用Priority_queue的基础容器的成员功能
- 如何子类可运行任何成员功能
- 共享类时如何隐藏成员功能文件
- 从C文件访问C 类成员功能