The code i am developing involves a lot of number crunching. I am interested in separating the functions which perform the numerical calculations from the class. My current implementation is of the following form.
1 2 3 4 5 6 7 8 9
class X:public XUtils{
};
class XUtils{
public:
// useful functions go here.
// This class consists only of functions
};
This seems to work great. The other option I have is to do something like the following
I do not have any good arguments for choosing one of above methods over the other. Also, is there a better way to do this ? Basically, I want to separate the number crunching part from the rest of the code so the code remains mostly clean and the dirty part of the code stays in a separate file.
I am interested in separating the functions which perform the numerical calculations from the class.
If the only reason you want to do this is to make these functions available to other classes as well, the first approach is enough.
1 2 3 4 5 6
class XUtils {/*...*/};
class YUtils {/*...*/};
class ZUtils {/*...*/};
class A: public XUtils,YUtils {/*...*/};
class B: public YUtils,ZUtils {/*...*/};
If you want to do this in order to be able to vary X's functions' implementation, you could do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//accurate calculations
class XUtilsAccurate {/*...*/};
//fast calculations
class XUtilsFast {/*...*/};
template <class XUtilsClass>
class X: public XUtilsClass {/*...*/};
int main()
{
X<XUtilsAccurate> x_acc;
X<XUtilsFast> x_fast;
//...
return 0;
}
The aggregation (pointer composition) approach is useful if you want to be able to change the implementation during run-time.
Suppose that, besides the Accurate and Fast implementations, you also have a BlitzFast implementation that is as accurate as the Fast one but twice as fast. The drawback is that it uses 5 times more memory, so you only want to use it in one or two speed-critical points of your application. Then, you could do something like this:
class XUtilsBase //abstract base class
{
public:
virtual ~XUtilsBase()=0;
virtualvoid doA()=0;
virtualvoid doB()=0;
//...
};
class XUtilsAccurate: public XUtilsBase {/*...*/};
class XUtilsFast: public XUtilsBase {/*...*/};
class XUtilsBlitzFast: public XUtilsBase {/*...*/};
class X
{
XUtilsBase * utils;
public:
X() {utils=new XUtilsAccurate;} //default is accurate
~X() {delete utils;}
void doA() {utils->doA();}
void doB() {utils->doB();}
void toAccurate() {delete utils; utils=new XUtilsAccurate;}
void toFast() {delete utils; utils=new XUtilsFast;}
void toBlitzFast() {delete utils; utils=new XUtilsBlitzFast;}
};
int main()
{
X x_acc;
X x_fast;
x_fast.toFast();
x_acc.doA();
x_fast.doB();
//...
x_fast.toBlitzFast();
x_fast.doA();
x_fast.doB();
x_fast.toFast();
//...
return 0;
}
EDIT: I just saw that.
cpluplusrat wrote:
Basically, I want to separate the number crunching part from the rest of the code so the code remains mostly clean and the dirty part of the code stays in a separate file.
Well, that's why we have header and source files :P
Thanks for the suggestions. Significant portion of my code is symbolically generated for speed. There are several functions with around 200 lines of code which are of the form
a[1]=b[1]*c[1]+d[1]/a[0];
I was hoping to avoid releasing this dirt laundry which is the heart of the code but absolutely unreadable for everyone including myself. Indeed, I can always make these functions a member of the class itself. But the size of the source file becomes unnecessarily large which is undesirable.