-1

Let us assume the following class:

class FileManipulator
{
    static InputTypeOne * const fileone;
    InputTypeTwo *filetwo;

    public:
    FileManipulator( InputTypeTwo *filetwo )
    {
        this->filetwo = filetwo;
    }
    int getResult();
};

FileManipulator uses data from both files to obtain output from getResult(). This means multiple iterations over filetwo and multiple constructions of FileManipulators via iterations for different InputTypeTwo objects. Inputs are, let us say, some .csv databases. InputTypeOne remains the same for the whole task.

The program itself is multi-modular and the operation above is only its small unit.

My question is how can I handle that static field in accordance with the object-oriented paradigm and encapsulation. The field must be initialized somehow since it is not a fixed value over different program executions. As far as I understand C++ rules I cannot create a method for setting the field, but making it public and initializing it outside of any class (FileManipulator or a befriended class) seems to me at odds with the encapsulation.

What can I do then? The only thing that comes to my mind is to do it in a C manner, namely initialize it in an isolated enough compilation unit. Is it really all I can do? How would that be solved in a professional manner?

edit

I corrected pointer to constant to constant pointer, which was my initial intention.

infoholic_anonymous
  • 929
  • 3
  • 12
  • 29

2 Answers2

0

You can write a public static method of FileManipulator that would initialize the field for you:

static void init()
{
  fileone = something();
}

And then call it from main() or some place where your program is being initialized.

pvgoran
  • 430
  • 3
  • 12
  • @infoholic_anonymous Why do you think so? – Constructor Dec 12 '13 at 10:22
  • @Constructor If you try to compile it you'll get a linker error. You must create the static variable before you will assign value to it. see http://stackoverflow.com/questions/185844/initializing-private-static-members – infoholic_anonymous Dec 12 '13 at 10:27
  • @infoholic_anonymous Of course, you should also write smth like this: `const InputTypeOne *FileManipulator::fileone;` in `*.cpp` file. And? – Constructor Dec 12 '13 at 10:32
  • @Constructor have you even read the question? I do say there that it seems at odds with encapsulation, and therefore might be unnecessarily risky. Normally you keep such things private to restrict access. I asked if making it global within compilation unit is the only solution. – infoholic_anonymous Dec 12 '13 at 10:37
  • @Constructor Also, it wouldn't work for a const field obviously, which is here the case. – infoholic_anonymous Dec 12 '13 at 10:42
  • @infoholic_anonymous I can't understand why this solution does not suit you. – Constructor Dec 12 '13 at 10:43
  • 1
    @infoholic_anonymous It would work. Don't confuse a constant pointer and a pointer to a constant. The last one is used in your code and it is not a constant. – Constructor Dec 12 '13 at 10:46
  • @Constructor my bad. I meant the first, sorry. :/ – infoholic_anonymous Dec 12 '13 at 10:54
  • Declaring a static variable like `const InputTypeOne *FileManipulator::fileone;` has nothing to do with; it's like a part of class definition (and should be done in a .cpp file related to the class). – pvgoran Dec 12 '13 at 11:45
  • @infoholic_anonymous if you want a constant pointer, you'll need to initialize it in the same .cpp file like this: `InputTypeOne *const FileManipulator::fileone = something();`. But that's not a good idea because you can't control when initialization will happen. What's the problem with having a **private** non-constant pointer? Noone from outside the class will be able to access it anyway. – pvgoran Dec 12 '13 at 11:47
0

One way of doing this which comes to mind is:

In the .cpp file

FileManipulator::fileone = NULL;

Then modify constructor to do the following:

FileManipulator( InputTypeTwo *filetwo,  InputTypeOne  *initValue = NULL)
{
    if(fileone == NULL)
    {
        fileone = initValue;
    }
    this->filetwo = filetwo;
 }

Or you could also define an init function and make sure to call it before using the class and after the CTOR. the init function will include the logic of how to init fileone.

Max Shifrin
  • 791
  • 7
  • 22
  • How would such init function work? I read http://stackoverflow.com/questions/185844/initializing-private-static-members as I cannot initialize static members in function bodies. – infoholic_anonymous Dec 12 '13 at 10:31
  • You can call a static function, from a none static function. The other way around won't work. What I suggest is similar to the code written in the constructor, just moved to a side function which will do the init only once. (any subsequent calls will do nothing if made) – Max Shifrin Dec 12 '13 at 11:55