I have written a class that reads in data from external files, manipulates the data, and then does a calculation.

The class is very long, particularly the constructor. This is where I read in data from external files and manipulate it in preparation for the calculation. I use template functions to do this.

I know that it would be better style to split the class up, but I am using it in combination with commercial code that I can't change. Also, people who will need to use the code need it to be one class + auxiliary files, so I'm restricted to this one class. To make the code more readable, I would like to store some of the code from the constructor in helper .cpp/.h files called "auxiliary.h/.cpp" and access it through functions.

Here's the problem:

1) Passing the template functions (member functions of the class) as function arguments to the auxiliary files. I can't do this, although I have heard it might be possible through something called "disambiguation." It is no problem to pass non-template functions and vectors and things like that... The template functions are the problem.

2) Also, even if I could pass the template functions as arguments to the functions that access the auxiliary files, my argument lists end up very long.

What I would like to know is, is there some way to make the auxiliary files "see" the class? I tried passing an INSTANCE of the class as an argument/parameter in each function that does stuff in the auxiliary files. But this results in "instanceName not declared in this scope" and it does seem like a rather circular/convoluted approach.

Any advice would be very appreciated. Thanks.

2

There are 2 answers

1
Marc Plano-Lesay On

You can probably make a template helper class instead of helper functions, passing a pointer to your first (the big one) class to it ?

1
DXM On

I know that it would be better style to split the class up, but I am using it in combination with commercial code that I can't change...

I may be missing more background to your specific situation but regardless of what third-party code you are using, you should be able to do whatever you want with your code that you wrote and it sounds like you wrote a lot.

You recognized your class is too big so next step is to break it apart into separate, more cohesive classes. Attempting to create other files/functions that simply modify the internal state of your one class that's already big, will make the code even bigger and you will spaghetti it up even more than it is now.

If the third-party library needs to operate with a single class that you writing, take a look at Facade design pattern. You can easily have what "looks like" a complex class to the outside, but internally it simply delegates the work any number of more specialized classes.

If you want one class to have access to a third-party library, you can do that too. Take a look at something such as Adapter design pattern.

Another advice I can offer is pick up this book on refactoring. It'll show you how you can take any code that looks way too complex and methodically apply steps to make it much more readable and maintainable. For example take a look at your data members, do any of them look like they belong to one group (i.e. they are often accessed/modified together)? If yes, consider moving them into their own class which provides minimal interface for getting/setting only what your main class needs.

This post doesn't answer your specific question, but take a step back, maybe there's a much better solution than breaking OO rules and good design principles just because of some constraints which may not really be there.