|
|
| Don't care if you call me stupid.. |
|
Kevin Epps
Member #5,856
May 2005
|
I just CANNOT think of how to do this for some reason. And since I don't really know the term for it, It's hard to search for what I'm looking for. Does ANYONE know what it's called to pass several values in one argument of a function in c++? Example: void func(arg1, val1 | val2 | val3, arg3); I think you're supposed to make the argument in question a type of unsigned. If that's so how do extract each value from that one argument? |
|
Steve Terry
Member #1,989
March 2002
|
That's a binary OR operation.
___________________________________ |
|
Kevin Epps
Member #5,856
May 2005
|
AHHH! ENUMERATION!!! Thanks so much, Steve! |
|
Thomas Fjellstrom
Member #476
June 2000
|
You dont even need the enum. #define foo 1 #define bar 2 #define baz 4 #define frob 8 // or const int foo = 1; const int bar = 2; const int baz = 4; const int frob = 8; Notice the pattern. You can also use shifts to make the values, if you think it helps understanding.. const int foo = 1; // << 0 const int bar = 1 << 1; const int baz = 1 << 2; const int frob = 1 << 3; Basically "flags" need to have one bit in the integer set to on, and the rest off for this to work properly. Powers of two just happen to have this property. -- |
|
Kevin Epps
Member #5,856
May 2005
|
Hey Thomas, That really didn't help me much. I don't have a problem understanding that part, it's how to declare the function that's going to use the flags. What I'm trying to do is make a function that's passed several flags that then write out a message based on the flags passed. I tried Steve's approach, but I get an error message saying that I can't convert a const int to an enum. Then I tried Thomas's approach somewhat, but I don't know if I'm declaring the function correctly.
And I called the function like this: show_credits(fade_buffer, "Project Manager", KEV | A_F, 300, 2); /*Is supposed to print out: Project Manager Kevin Epps Adam Friday*/ But "Adam Friday"'s the only text shown under Project Manager. (Also, if I change the conditionals back to (nam & whatever), then the all print regardless of the flags passed thru. |
|
Carrus85
Member #2,633
August 2002
|
Well, there are two problems with that code:
Edit: Ah, just noticed the last line of your post. Yes, the &'s are required. They just don't work if you don't set the nam value correctly! The codes must be powers of two in order to be set in the bitmask properly. Oh, and just as a note, it might be wise to make your function take a const unsigned int nam instead of a const int, to avoid any potential weirdness with the signs.
|
|
Kevin Epps
Member #5,856
May 2005
|
THERE we go!! It works now! I was missing the point about the powers of two bit. Thanks man. |
|
Bob
Free Market Evangelist
September 2000
|
It sounds like you actually want a list of things in general, not just list of bits. What's wrong with passing an array (or vector) of string? -- |
|
Kris Asick
Member #1,424
July 2001
|
Think of it this way, Kevin: Computers work on bits, 1's and 0's, thus each number is a combination of bits. For instance: 33 = 00100001, 145 = 10010001, 26710 = 0110100001010110 What you're doing is a form of "bitmasking" usually called "flags" or "tags". The idea is that one variable stores multiple true/false values in each bit. The reason why you need to use powers of 2 is because each power of 2 only touches one bit: 1 = 00000001 2 = 00000010 4 = 00000100 8 = 00001000 16 = 00010000 32 = 00100000 64 = 01000000 128 = 10000000 So let's give these all values: #define FLAG_SOLID 1 #define FLAG_PHASED 2 #define FLAG_IMMUNE 4 #define FLAG_SPHERICAL 8 #define FLAG_AI 16 #define FLAG_AUTOMOVE 32 #define FLAG_SPECIAL 64 #define FLAG_PLAYER 128 Now, you simply use a bit-wise OR operation, the | symbol, to combine them, which effectively does something like this: 0100101110100101 | 1010100101110100 = 1110101111110101 Thus if you did this: variable = FLAG_PHASED | FLAG_AI | FLAG_SPECIAL ...the result is: 00000010 | 00010000 | 01000000 = 01010010 Then when you want to check if a particular flag is set or not, you use a bit-wise AND operation instead, which is the & symbol: if (variable & FLAG_AI) ProcessAI(); if (variable & FLAG_SPECIAL) TriggerSpecial();
Make sense now? You'll also note that using '+' instead of '|' works the exact same under these circumstances, provided you don't accidentally add the same flag more than once. (But you can't use '-' in place of '&'!) --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
|
Kevin Epps
Member #5,856
May 2005
|
thanks for the input. I got working after Carrus's explanation and I understand it now. I had just forgotten about setting up the flags to be powers of two. Thanks guys for you help. |
|
Johan Halmén
Member #1,550
September 2001
|
Stupid! Couldn't resist. Actually you're not. That was a good question. And remember, there are no stupid questions, just a lot of inquisitive idiots... Sorry again. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
|
Fladimir da Gorf
Member #1,565
October 2001
|
class Clazz { Clazz operator | (const Clazz &other) { return //.. } }; const Clazz OPT1( ... ); const Clazz OPT2( ... ); const Clazz OPT3( ... ); callFunc( OPT1 | OPT2 | OPT3 );
Tee hee OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori) |
|
KnightWhoSaysNi
Member #7,339
June 2006
|
Thank you for asking this question. It has helped me too. Before reading this I had a few functions that were something like this: void drawtext(srting text, int size, bool bold, bool italic, bool underline, bool strikeout, bool etc...) |
|
|