|
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
|
|
|
|
|
I have searched the world for a solution to format a CString value of the format "longdate" to "shortdate".
"YYYY-MM-DD hh:ss" to "YYYY-MM-DD".
I'm not using .net.
It should be simple, but not for me...
/LK
|
|
|
|
|
|
Yes thanks!
I found a solution:
COleDateTime dt;
CString theDate=pProject->getProjectCompletionDate();
dt.ParseDateTime(theDate);
if (dt.GetStatus()==COleDateTime::valid)
theDate=dt.Format(VAR_DATEVALUEONLY);
else
theDate="";
|
|
|
|
|
Well, if the format is fixed, you can simply truncate the string at the space between the day digits and the hour digits.
CString sTD = _T( "2006-01-02 12:34" );
sTD.Delete( 10 ); Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|