类和文件读取
用于文件读取的ifstream类型的对象可以是类的静态成员吗? 我想读取一个文件并将每一行存储在我创建的类的对象数组中。 我希望文件读取对象属于整个对象数组,而不是该类的单个实例。
Can an object of ifstream type used for file reading be a static member of a class? I want to read a file and store each line in an array of objects of a class i have created. I want the file reading object to belong to the entire array of objects instead of one single instance of the class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您正在询问有关 C++ 的问题 - 请在以后明确说明。 答案是,是的,当然可以——你尝试过吗? 如果您遇到过问题,请发布导致问题的代码。 请注意,对于所有静态成员,您需要在 .cpp 源文件中定义它:
I assume you are askingng about C++ - please be explicit in future. And the answer is, yes, of course it can - have you tried it? If you have, and you experienced problems, post the code that causes the problems. Note that as for all static members you will need to define it in a .cpp source file:
由于未指定语言,我只能将您重定向到基本设计模式:单例模式 。 请参阅@Neil 的 C++ 答案。
As the language is not specified, I can only redirect you to the basic design pattern : Singleton pattern. Refer @Neil answer for the C++.