views:

32

answers:

2

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.

+2  A: 

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:

// a.h
class A {
   static std::ifstream mIfs;
};

// a.cpp
std::ifstream A::mIfs;
anon
A: 

As the language is not specified, I can only redirect you to the basic design pattern : Singleton pattern. Refer @Neil answer for the C++.

aJ