|
This is a Qt issue as far as using the toInt function. And as the documentation (QString Class | Qt Core 5.15.17[^]) clearly shows, it handles numbers in any base from 2 to 36.
modified 4-Sep-24 6:16am.
|
|
|
|
|
I'm maintaining old code.
I can't use enum class.
Do you wrap your enums in namespace to kinda simulate enum class ?
or is there a pattern Ì don't know about to make regular enum safer ?
Thanks.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
You don't need to go as far as a namespace, just a struct will do.
struct Color {
enum value { Red, Yello, Blue };
};
int main()
{
Color::value box = Color::value::Red;
}
If you want to be able to print Color::Red as a string, it's a bit more involved
#include <iostream>
struct Color {
enum hue { Red, Yellow, Blue } value;
std::string as_string() {
std::string color;
switch(value) {
case Red : color = "Red"; break;
case Yellow : color = "Yellow"; break;
case Blue : color = "Blue"; break;
};
return color;
}
Color(Color::hue val) : value(val) {};
bool operator==(const Color&other) {
return value == other.value;
}
friend std::ostream& operator<<(std::ostream& os, const Color& color);
};
std::ostream& operator<<(std::ostream& os, const Color& color)
{
os << color.value;
return os;
}
int main()
{
Color x = Color::Red;
Color y = Color::Blue;
std::cout << x << '\n';
std::cout << x.as_string() << '\n';
if(x == y)
return 1;
else
return 0;
}
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
modified 29-Aug-24 11:50am.
|
|
|
|
|
ahhhh yes, I've seen that before.
thanks.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
Last time I did hardcore C was a while back. Before the 128-bit days. Ok, cool. But, I got a silly question when it comes to printing a 128-bit integer. You see online examples saying just do a long long cast and call it a day bro, but then they use small numbers. Which obviously works for them because it's a small number.
But, I figure hey, I'll try it for poops and giggles. As you'd might expect the number is never correct.
#include <stdio.h>
int main()
{
__uint128_t u128 = 34028236692093846346337460743176821145LL;
printf("%llu\n", (unsigned long long)u128);
return 0;
}
The above don't do it. Now, I could bit shift to get around any limits, but I ultimately need the output formatted with comma separators, so doing bit logic would cause issues when putting humpty dumpty back together again.
So, um... anyone know how to print a 128-bit number in C? Preferably portable C.
Jeremy Falcon
modified 28-Aug-24 17:19pm.
|
|
|
|
|
As far as I know, the C standard does not specify anything about 128 bit integers. _uint128_t is a GCC extension, also supported by clang. Maybe MSVC too? As far as I know, there's no printf format code for 128 bit integers either. See here for a possible solution: https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
Yeah that's what I was afraid of. If it wasn't for the formatting, I'd just slice it up as two 64s to be portable for printf. I may just have to roll my own formatting for something I'm working on.
I mean, I could just forget about 128-bit support, but no cool points for that.
Thanks btw.
Jeremy Falcon
|
|
|
|
|
|
Never even heard of it. Does it handle localization? If so, you're totally my hero.
Jeremy Falcon
|
|
|
|
|
It should. Per above reference:
Quote: The decimal point character (or string) is taken from the current locale settings on systems which provide localeconv (see Locales and Internationalization in The GNU C Library Reference Manual). The C library will normally do the same for standard float output.
Mircea
|
|
|
|
|
Schweet. I'll have to check it out. Thanks man.
Jeremy Falcon
|
|
|
|
|
One is glad to be of service.
Mircea
|
|
|
|
|
Hi,
I want to create my own border in the client area of a dialog and could freely move it on the parent.
Can you plz suggest any refence?
|
|
|
|
|
Your question is not very clear. You can easily add a border to a dialog with a static control. And what do you mean by "and could freely move it on the parent."
|
|
|
|
|
You'll have to draw a frame manually (CDC::Rectangle) and handle all the mouse click events to be able to move it.
What exactly are you trying to do ?
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
A binary file is a file that contains bits. Every 32 bits make a number (several digits/symbols with no space between them) or a word. A number can then be transformed and become an integer (several digits/symbols) or a char (just one symbol). Character set (Unicode for example) has to do with how a word of bits becomes a char. Is that how it works?
[edit] A text file is an inefficient way to store numbers because each digit is one char
modified 26-Aug-24 9:00am.
|
|
|
|
|
Calin Negru wrote: Is that how it works?
yes, more or less.
Every file is just a series of bits.
It's up to the user (programmer) to interpret how the series of bits is converted to something practical (text, numbers, ... )
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
Generally speaking, all files contain bytes. What interpretation you put on those bytes is up to you. A file containing the hex bytes 61 62 63 64 (without spaces ) might be interpreted as a 32 bit integer of value 1684234849 (assuming little endian byte ordering) or the 4 characters abcd. Interpretation is everything.
Text files may be slower than binary files to read/write, but they do have the advantage of being processor agnostic. For example in 32 bit mode, structs have different padding on ARM and x86, so given
struct S {
}; If you have a data file containing an array of struct S , you can't just copy the data file from an x86-32bit system to an ARM-32bit system and assume that the offsets for the member is going to match. That's also true for x86-32 to x86-64. Even if the struct members don't have different sizes (e.g. a long may have 32 bits or 64 bits), they may have different padding requirements between 32 and 64 bit systems.
Then there's the whole little endian vs big endian situation.
But a text file can be read by any system, without any conversion routines.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
A binary file is a stream of bits that can be interpreted by your code in any way it wants. 32 bits does NOT mean it's a number. Those exact same 32 bits can be four ASCII characters, two 16-bit UTF-16 characters, four bytes, two short integers, either signed or unsigned, or one signed or unsigned integer, or 32 Booleans encoded into 4 bytes, or ...
Bytes in a file represent nothing until the code that reads the file assigns meaning to them.
Text file are just streams of bytes, just like all files are. Efficiency is subjective.
|
|
|
|
|
Thanks for your feedback. I think I understand.
|
|
|
|
|
I`m trying to place a derived class object into an c++ array containing base class objects. You suggested I should use the following
unit* LUnits = new unit[2];
soldier * S = new soldier();
LUnits[0] = dynamic_cast<unit*>(S);
sailor* AB = new sailor();
LUnits[1] = dynamic_cast<unit*>(AB);
sailor* tar = dynamic_cast<sailor*>(LUnits[1]);
The code above doesn`t compile with VS2022.
This compiles:
sailor* tar = dynamic_cast<sailor*>(&LUnits[1]);
I can't find a solution for the following
LUnits[0] = dynamic_cast<unit*>(S);
The compiler doesn't recognize the = operator. The errors are E0349 no operator "=" matches these operands and
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'unit *' (or there is no acceptable conversion)
Any ideas how to move forward?
modified 24-Aug-24 6:31am.
|
|
|
|
|
I think you are missing an indirection level:
unit* LUnits = new unit[2];
Lunis is a normal array. Elements are unit objects
LUnits[0] = dynamic_cast<unit*>(S);
This fails because LUnits[0] is a unit , not a unit*
The first line should be:
unit** LUnits = new unit*[2];
Mircea
modified 23-Aug-24 13:45pm.
|
|
|
|
|
unit* LUnits = new units[2] creates an array of 2 unit , not an array of 2 pointers to unit.
You probably want a double dereference:
unit** LUnits = new units*[2]; soldier* S = new soldier;
unit[0] = S;
It feels like there should be a better way to do that.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
modified 23-Aug-24 13:54pm.
|
|
|
|
|
Thank you guys indirection was the problem
modified 23-Aug-24 16:56pm.
|
|
|
|
|
I am adding a new object to an existing object.
In QtCreator, I have an option to add it /pass it as a class new parameter.
The other option is to add new object as a .so library.
I am asking for (academic) evaluation / discussion of each option,
as a C++ task , nothing to do with Qt.
I will appreciate your time , however, off subject ,
immature flaming responses will be as always ignored.
Cheers
|
|
|
|