X
xuatla
Hi,
I am not sure whether I describe the subject correctly.
What I want to do is for a matrix class
---------------------------
class CMatrix
{
private:
size_t m, n;
double **nodes;
private:
bool checkSize(...); // check size and initialization if needed.
public:
CMatrix operator + (const CMatrix& m1);
CMatrix operator += (const CMatrix& m1);
......
}
CMatrix CMatrix:
perator+(const CMatrix& m1)
{
CMatrix res(m1);
for ....
return res;
}
-----------------------------
If I need to perform a lot of addition and use + (discarding += first,
since I want to write something like a+b+c-2*d...), then the program
will do a lot of memory allocation and release (for instantiation of
"res" in "+"). I want to know:
1. is the instantiation of class fast or not? (compared with some math
operations, e.g., one + between two double values).
for memory allocation, I use:
nodes = new double*[m];
for (size_t i=0; i<m; i++) nodes=new double[n];
2. is it good or not to set up a global variable for the above "res" so
that I can skip the instantiation part? (suppose the memory size is not
a problem)
3. how to do (2)? I think about static member:
static CMatrix _mattmp;
do I need to put this inside the class or outside?
Thanks a lot!
Regards,
X
I am not sure whether I describe the subject correctly.
What I want to do is for a matrix class
---------------------------
class CMatrix
{
private:
size_t m, n;
double **nodes;
private:
bool checkSize(...); // check size and initialization if needed.
public:
CMatrix operator + (const CMatrix& m1);
CMatrix operator += (const CMatrix& m1);
......
}
CMatrix CMatrix:
{
CMatrix res(m1);
for ....
return res;
}
-----------------------------
If I need to perform a lot of addition and use + (discarding += first,
since I want to write something like a+b+c-2*d...), then the program
will do a lot of memory allocation and release (for instantiation of
"res" in "+"). I want to know:
1. is the instantiation of class fast or not? (compared with some math
operations, e.g., one + between two double values).
for memory allocation, I use:
nodes = new double*[m];
for (size_t i=0; i<m; i++) nodes=new double[n];
2. is it good or not to set up a global variable for the above "res" so
that I can skip the instantiation part? (suppose the memory size is not
a problem)
3. how to do (2)? I think about static member:
static CMatrix _mattmp;
do I need to put this inside the class or outside?
Thanks a lot!
Regards,
X