|
Here[^]
Relatively it's a simple question, wondering it required so much explantion. Think it this way,
Floats are meant for storing percision data, if you store a float value in an int? wont it loose the precision? But you are not going to do that right? the same way unless or otherwise you manually do something like :
<br />
void myFunction(size_t sz)<br />
{<br />
<br />
}<br />
and pass something like
myFunction(-100);
There's no possiblity of loosing data. In short, It looks you aren't sending negative values in your code so please ignore the warning.
He's become a household word in the Lounge. A whole new phraseology has evolved. Post a link or reply with a smiley and rose, and you've made a "Satipsism". So what? It's an interesting thing about the Internet, the evolution (as in change, not progress) of tone, quality, terminology, etc.
-Marc Clifton.
Best wishes to Rexx[^
|
|
|
|
|
Hi VuNic,
I am always using unsigned int as data type. Example,
unsigned int i = <some value="">;
size_t new_value = (size_t)i;
I am not sure whether there will be any data lost if I convert i to new_value as size_t type. I can understand convert to signed int will cause data lost. What about unsigned int?
Looks like we are talking about two different things.
regards,
George
|
|
|
|
|
if your sure you will not go between 32-bit and 64-bit with this code someday then an explicit cast should eliminate the warning. Let the compiler know the conversion is not implicit and it will take your word for it that you know what your doing.
size_t First=45;
int Second=67;
unsigned int Third=98;
// Damned if you do
Second=(int)First;
Third=(unsigned int)First;
// Damned if you don't
Second=static_cast<int>(First);
Third=static_cast<int>(First);
|
|
|
|
|
Hi bob16972,
I am using C and I can not use static_cast template class. I am wondering if I am building application for 32-bit platform only, will there be any data lost if I convert size_t to unsigned int? (not signed int)
regards,
George
|
|
|
|
|
The static_cast was in my post to appease the C++ purists. I use the C-style casts myself.
George_George wrote: I am building application for 32-bit platform only, will there be any data lost if I convert size_t to unsigned int?
No.
Clarification:
I included signed int in my original post without thinking as there is the potential to misinterpret or lose data with that conversion. size_t is supposedly an unsigned int so the conversion from size_t should be pretty clean in a 32-bit only world.
|
|
|
|
|
Hi bob16972,
static_cast is an operator in C++, how do you think we can use it in pure C program?
regards,
George
|
|
|
|
|
Considering this is a C++ forum, I assumed you were using .cpp files. If you are truly using .c files then my apologies. I haven't compiled a "pure" C program since 1992 or 1993.
Either way, I don't use static_cast myself so no harm no foul.
|
|
|
|
|
Thanks bob16972,
It is ok.
regards,
George
|
|
|
|
|
If you don't care about 64-bit compatibility warnings, why not turn the 64-bit compatibility check
off in the compiler settings?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for your new idea, Mark!
Do you know how to turn 64-bit compatibility back? Which compiler settings (macros) should I look into?
regards,
George
|
|
|
|
|
I'm still not sure if that's why you're getting the warning. I thought you stated you tried
unsigned int instead of int in an earlier post - if that's the case then it's probably a 64-bit
compatibility warning.
If you go to the project properties, the C++/General page should have the "detect 64-bit portability
issues" switch.
There's also #pragma warning for turning on/off warnings for sections of code.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for your advice, Mark!
regards,
George
|
|
|
|
|
size_t in unsigned but int is signed.
Steve
|
|
|
|
|
Hi Stephen,
If I am converting size_t to unsigned int on 32-bit platform, are there any risks of data lost?
regards,
George
|
|
|
|
|
It depends on how the conversion is done. Why not use size_t directly. If you can't perhaps unsigned int would be a closer match.
Steve
|
|
|
|
|
Good idea Steve,
I will try to change the function signature (parameter type).
regards,
George
|
|
|
|
|
Hi all;
I am (new to c++) trying to write a program which in part of it has a list of objects with a few variables.(pseudo code)
lets say:
class cObj
{
private:
int objNum;
string objName;
}
list<cobj> ListObj;
and we have three objects
obj1 ( onjNum =1 , ObjName = One)
obj2 ( onjNum =2 , ObjName = Two)
obj3 ( onjNum =3 , ObjName = Two)
and i want to search all the objects that have the name "Two".
result should be: &Obj2 and &Obj3 (or pointers)
how can i do it?
thanks a lot;
Beri
|
|
|
|
|
If you store then in a vector, you can write a functor to do the search you are after and use the built in search algorithms.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
thanks for you kind help!
i see, vector has a random access to any position.
but if i had to use a list(not only because i wrote a very long code :P)?
isn't there any option to check something like this:
lets say i have a function:
bool mysearch( string _objName)
{
int found = 0;
list::iterator iter;
iter = list.begin();
while(iter!=list.end())
{
if(iter.objName== _objName) //iter.objName is my problem - how to implement
found++ // get the times objName found
}
}
thanks a lot;
Beri
-- modified at 3:12 Saturday 28th July, 2007
|
|
|
|
|
You should overload the == operator and use the find algorithm.
<br />
class cObj<br />
{<br />
int objNum;<br />
string objName;<br />
Below,you can actually have the int type as well that you can check for your "2" directly than passing it from an object. But in case you want to change the search condition sometime later, this doesnt help. Better pass the object and get the member from it.
<br />
bool operator == (const cObjSent)
{ <br />
return ((objNum==cObjSent.objNum));<br />
}<br />
}<br />
<br />
void main()<br />
{<br />
list <cObj> cObjObject;<br />
list <cObj> ::iterator cObj_iter;<br />
<br />
cObj_iter = find(cObjObject.begin(), cObjObject.end(), YOUR_COBJ_SEARCHOBJECT_HERE [Like for eg:your obj2]
);<br />
<br />
}<br />
<br />
He's become a household word in the Lounge. A whole new phraseology has evolved. Post a link or reply with a smiley and rose, and you've made a "Satipsism". So what? It's an interesting thing about the Internet, the evolution (as in change, not progress) of tone, quality, terminology, etc.
-Marc Clifton.
Best wishes to Rexx[^
|
|
|
|
|
thanks a lot Marc!
May The Force Be With You...
|
|
|
|
|
Hi all i want to make progress bar inside list control(report style). I have found various articles regarding this but they don't fulfil my requirements.
i have made a sdi application and that sdi window is divided with a vertical splitter controls in two parts.Each veiw is derived from CFormVeiw and have a list control in it.I want to make progress bar in my list control and that progress bar should start when i click a button on toolbar.
How to do this.....
Thanks in advance
|
|
|
|
|
There are very good examples regarding this on codeproject i dont remember exact names but try something XListControl you will get it.
|
|
|
|
|
|
Hi,
I want to write a program that when I input integer then the output will have commas if it has more than three digits. For example,
-2007 and 2000000 would be printed as -2,007 and 2,000,000 respectively..
# include <iostream>
# include <iomanip>
using namespace std;
int main ()
{
double digit;
cout <<" print it with comma "<< endl;
cin >> digit;
cout <<setiosflags(ios::fixed| ios::showpoint)<< setprecision(3) <<endl;
cout <<digit <<endl;
return 0;
}
dalmar mahamoud
|
|
|
|