i have 10 different classes-objects that are controled by a another class{call it Central class). All 10 classes need to write to the same file(not at the same time). Is it better to have central class control the stream and pass it to the other classes, or each class should implement it's own stream{for as long it is needed}...and all of this streams write to the same file...

thanks for your help

If your I/O activities are performed by a central class you can be rest assured that at a time only a single class or a class function is writing to a file since the the opening and closing of the file stream would be under the control of the central class.

It would also save you a lot of checks on the file stream to see if they are already open or not since there is a single controlling authority.

All this being said, it all boils down to what and how you are trying to implement your requirements.

i have 10 different classes-objects that are controled by a another class{call it Central class). All 10 classes need to write to the same file(not at the same time). Is it better to have central class control the stream and pass it to the other classes, or each class should implement it's own stream{for as long it is needed}...and all of this streams write to the same file...
thanks for your help

Well, considering the information you've provided I might implement some type of interface that uses Polymorphism. You could just declare and define a protected function for the file stream binded to the base class and allow all inherited class access to this function. Here is an illustration:

class COMbase {
        public:
           COMbase();
           ~COMbase();
           virtual void myName()=0;
 
       protected:
           int openFile(string filename);
           void closeFile();
 
       private:
           ifstream in;
};
 
int COMbase::openFile(string filename) {
        if (in.open(filename))
                return 1;
        return 0;
}
void COMbase::closeFile() {
        in.close();
}
 
class COM1 : public COMbase {
          public:
                  COM1(){}
                  ~COM1(){}
                  void fileOpen(string name) {
                          if (COMbase::penFile(name)) 
                          cout << "COM1-File could not be opened" << endl;
                  }
                  void fileClose() {COMbase::closeFile();}
                  void myName() {cout << "COM1" << endl;} 
 
          protected:
          private:
};
class COM2 : public COMbase {
         public:
                 COM2(){}
                 ~COM2(){}
                 void fileOpen(string name) {
                        if (COMbase::penFile(name)) 
                        cout << "COM2-File could not be opened" << endl;
                 }
                 void fileClose() { COMbase::closeFile();}
                 void myName() { cout << "COM2" << endl; }
 
        protected:
        private:
};

The base class (COMbase) contains a pure virtual member function so you should only create pointers of type COMbase which allow you to pass any inherited class to be pointed to by the base pointer (i.e COMbase *comptr = &Some_in_instance To allow for instances of COMbase, you'd omit the "=0" on the virtual function and provide a block of code for it.

Under normal circumstances, you'd simply use the sub classes to open up a file via the central or base class COMbase. I hope I helped a little and good luck.

LamaBot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.