|
returning value from array is very simple
we code it as return type is always the same
long double operator()(int x)
{
switch(type)
case 1:
return (long double)cdata[x]; //if cdata is char
case 2:
return (long double)sdata[x]; //if sdata is short
..etc
}
the problem is that we need to code either a = vec(0) and vec(0) = a, where a is long double.
long double &operator()(int x)
{
//need to get a reference to type long double from all possible types as char,short,float,...
}
9ine
|
|
|
|
|
make a generic base class for that types you put in it (like CObject for instance), so that your subscript will be an instance of CObject. then, if you want to performs operations on these types, such as + , += , etc..., then each type/class that is supposed to be placed in the array must implement the operators...
the best you can do is providing virtual operators without implementation within the object base class...
one last thing. what' you're trying to achieve don't seem to be that good. what are you doing exactly ? and what for ?
TOXCCT >>> GEII power [toxcct][VisualCalc 2.24][3.0 soon...]
|
|
|
|
|
If all of you suggesting to use something like
Carray
Cchar : public Carray
Cshort : public CArray
Cfloat : public CArray
9ine
|
|
|
|
|
No, this is very important thing I'm trying to achieve.
I need to code for saving memory of loading large amount of ADC data.
This data can be of 8bit 16bit. Also I want to support not ADC data as floating numbers.
Due to the need of performing signal processing I want to be all the data converted to say double or long double to get more precision on arithmetic ops.
If we load ADC data which is 16bit quantized we convert it to double, same for 8bit data. And do some high presision calculation and plot results etc...
If our data sampled at 1000Hz 16bit for a 10hours we must allocate 36000*1000*sizeof(double) = 288Mb. But If we keep it as 16bit numbers converting to double and back at operator() which we need for saving also this will requier 36000*1000*sizeof(short) = 72Mb.
Great if this can be achieved with one class.
Another very good approach is to not load data from hdd at all keeping just FILE pointer, will slow than with memory, but reasonable if we got 2Gb of data that is it will go to swap file.
9ine
|
|
|
|
|
Hi,
why can't or won't you use templates
this could be a solution
codito ergo sum
|
|
|
|
|
I think that templates will be of no use in that situation either child classes for handling different types of data
class
{
short *sdata
long double &operator()(int x){
return (long double)sdata[x];
}
}
and saving to it value will be impossible
class vec;
long double a=10.0;
vec(0) = a; //?????????????????????????
9ine
|
|
|
|
|
Hi,
I think there is a flaw in your concept:
I believe you would like to use the following syntaxis:
MyArr arr;
long double ldData = arr(1);
ldata *= 2;
arr(1) = lData;
For the first arr(1) you only need a right value
long double operator()(int x)
{
return static_cast<long double>(sdata[x]);
} but for the second arr(1) you need a left value
short& operator()(int x)
{
return sdata[x];
}
The compiler can't decide which to use:
You can't combine the two functions like this :
long double& operator()(int x)
{
return (long double&)sdata[x];
} because the data allignment will be fault.
I think you best use a get (returning a long double) and a set (setting a short).
There is a solution with templates but you might (I certanly do ) consider it to be worse
class arr
{
public:
arr() {
pData = new short[5];
for(short sIndex = 0; sIndex < 5; sIndex++)
pData[sIndex] = sIndex;
}
template<typename T>
T operator()(int x)
{
return static_cast<T>(pData[x]);
}
short& operator()(int x)
{
return *(pData + x);
}
short* pData;
};
int _tmain(int argc, _TCHAR* argv[])
{
arr a;
double long lData = a.operator()<double long>(1);
lData *= 2;
a(1) = static_cast<short>(lData);
return 0;
}
codito ergo sum
|
|
|
|
|
Well Guys, ive got a query that why does crystal report control initialization is very slow. im facing this problem since ive installed crystal reports versions 10 n 11. it had better speed than 11 when i was using 9 version. if anyone knows the solution, please send me email at prog_irfan@yahoo.com
will b glad n thankful.
Irfan Shahid
|
|
|
|
|
9ine wrote: I'm writing 1D array class and want to overload operator () that is we can retrieve data from class and save it to it
Something like:
class vec
{
public:
vec()
{
data = new long double[5];
}
~vec()
{
delete [] data;
data = NULL;
}
long double &operator[]( const int nIndex )
{
return data[nIndex];
}
private:
long double *data;
};
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
|
|
|
|
|
you've written precisely what I need but the key thing is in private section there will be:
char *cdata;
short *sdata;
float *fdata;
long double *ldata;
how can we handle this in one &operator() and class
9ine
|
|
|
|
|
9ine wrote: how can we handle this in one &operator() and class
Short of using a template, I do not know of a way. If you added another operator[] to the class, the compiler will complain because the formal parameter list between the two would be identical.
Now if you wouldn't mind replacing operator[] with an actual method (e.g., getChar() , getFloat() ), you could get this to work.
Templates are not as difficult as you might think. Consider:
template <class T> class vec
{
public:
vec()
{
data = new T[5];
}
~vec()
{
delete [] data;
data = NULL;
}
T &operator[]( const int nIndex )
{
return data[nIndex];
}
private:
T *data;
};
...
vec<char> c;
c[0] = 'z';
char q = c[0];
vec<float> f;
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
-- modified at 12:31 Thursday 23rd February, 2006
|
|
|
|
|
--------------------------------------------
vec<char> c;
c[0] = 'z';
char q = c[0];
vec<float> f;
--------------------------------------------
I need to code this situation:
long double lA;
vec<char> c;
vec<short> s;
lA = c[0]; //casted from char to long double in operator
lA += 5.0;
c[0] = lA; //casted from long double to char in operator
lA = s[0]; //casted from short to long double in operator
lA += 10.0;
s[0] = lA; //casted from long double to short in operator
//same for vec<float>,vec<double>
The value outside the vec class (that is extracted from it) is always the same type long double but when we save it back it must be converted to class type (char,short,...)
9ine
-- modified at 5:00 Friday 24th February, 2006
|
|
|
|
|
9ine wrote: The value outside the vec class (that is extracted from it) is always the same type long double but when we save it back it must be converted to class type (char,short,...)
Why? The largest value you'll be able to store (in a char ) from the long double is 128. Is this acceptable?
Perhas you should explain in detail exactly what your goal is. Then we might be able to offer a more viable solution.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
the deal is about ecg data. This is saved as ADC units with presicion 8,16 bits and if we convert it to long double lu[x] = c[x]/UmV and back after calculations (which retain its original range) c[x] = UmV*lu[x].
Keeping it in its original units will save a lot of memory.
It's a pity there is no overloading on return value, may be this a drawback in C++.
9ine
|
|
|
|
|
9ine wrote: ...if we convert it to long double lu[x] = c[x]/UmV and back after calculations (which retain its original range) c[x] = UmV*lu[x].
I'm not sure I follow you. If you have 12.34 stored in some long double variable, assigning that to a char variable will result in only the value 12 being stored, effectively losing the .34. Is that an issue to be concerned with?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
that is simple quantizing issue. If our ADC data get -128 127 range say over 1mV range then 127 will be 1.0 long double, the formula in that case is ADCval/127 and we get long double values -1.0 1.0 performing computation on them and converting them back to char multipluing by 127, 0.5 long double will be 0.5*127 char, The value higher than 1.0 will be saturated to 1.0
This simple as we can operate on RGB in OpenGL either int 0-255 or float 0.0-1.0
9ine
|
|
|
|
|
9ine wrote: how can we handle this in one &operator() and class
You can't, a C++ is (on purpose) unable to overload functions based on their return value.
You can only overload based on parameters.
So you are forced to rethink your design.
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation."
-- Caius Petronius, Roman Consul, 66 A.D.
-- modified at 5:20 Friday 24th February, 2006
|
|
|
|
|
I wanted to get code for vector class as small as possible, as modification of the lot of other code will depend on it. Last resort in this situation will be set/get methods, which is much awkward compared to a=v(x); v(x)=a;
9ine
|
|
|
|
|
9ine wrote: a=v(x); v(x)=a;
You can have this. Easy.
What you can't have is C++ selecting the correct overload based on the type of the return value that fits your assignment target.
That would be asking a little too much, as it would either be quite fuzzy or stop C++ from doing implicit casts.
And that would break compatibility to C-code and annoy me.;P
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation."
-- Caius Petronius, Roman Consul, 66 A.D.
|
|
|
|
|
How about using void pointers?
void * m_Data;
void * operator[]( const int nIndex )
{
return m_Data+(sizeof (WHATEVER WE ARE CURRENTLY USING)*nIndex);
}
a = (double)*(vec[0]);
or
a = (char)*(vec[0]);
William
|
|
|
|
|
Engberts wrote: void pointers
"We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation."
-- Caius Petronius, Roman Consul, 66 A.D.
|
|
|
|
|
I want to put a process in the startup folder so it can run in the background. (No console) Does anybody know how to make it run in the background.
Kelvin Chikomo
|
|
|
|
|
|
Thanks dude, i wil just create a process terminates on its own , this same process will create the proper process in the background.
Kelvin Chikomo
|
|
|
|
|
theprinc wrote: ...run in the background.
This is Unix terminology. Windows is a multitasking OS, so there is no concept of a processing running in the background or in the foreground. If a process is running, it is running, period.
Now, a process with no UI is a totally different matter.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
|
|
|
|