我想编写一个用于从文件读取和写入对象的类。我使用模板关键字来实现这个目标。
template<class T>
class A{
private:
std::string filePath;//This will use to indicate the path of proper file.
public:
void save(T a){
//write the given object to the file. The path is fetched by filePath
}
std::list<T> read(){
//read the file and put each line in the list. The path is fetched by filePath
}
A(std::string path):filePath(path){}
};
如果我使用这种方法,当我将它与 int 或 float 等原始类型一起使用时,一切都很好。但是当我想使用对象时,问题就出现了,例如圆形、矩形。我应该怎么做才能解决问题