I was working on a project in C++ and found myself needing a method that determined what phase a substance was in (solid, liquid, or gas). There are only three possibilities here; I find myself in similar situations a lot, and while it seems obvious to just use integers (0, 1, 2 or 1, 2, 3), I've found that in practice this is unclear compared to Booleans. The other possibility I thought of was to make three methods, isLiquid, isGas, and isSolid, that each return a Boolean, but that seems inefficient. I was just wondering if anyone knows which one of these two possibilities is better, or if there's another idea I'm missing.
Asked
Active
Viewed 97 times
-1
-
15Discover `enum`. – n. m. could be an AI Jul 10 '17 at 17:38
-
10Are you familiar with [enumerations](http://en.cppreference.com/w/cpp/language/enum)? – François Andrieux Jul 10 '17 at 17:38
-
@n.m. won't that be just the first choice he's thinking about? – Alpha Mineron Jul 10 '17 at 17:38
-
Pretty new to programming, I've heard of enums but don't know what they are. looking at them now – Allball103 Jul 10 '17 at 17:39
-
5@AlphaMineron There is a huge difference between using an `enum` and "using an `int` and remembering what specific values mean, as well as remembering that you should try to stay within the range of defined values". – François Andrieux Jul 10 '17 at 17:40
-
1@Allball103 Tip: If you don't know something, look it up, it might help you in the future. – Rakete1111 Jul 10 '17 at 17:40
-
1@Rakete1111 you're probably right but if i looked up every coding term I saw I'd never get to actually write programs. – Allball103 Jul 10 '17 at 17:42
-
@FrançoisAndrieux Oh yea, I do get that. But i just pointed it out because he was thinking of using a sequence (0,1,2) like enum. I hope I'm not missing something big. – Alpha Mineron Jul 10 '17 at 17:42
-
1@AlphaMineron No, especially if using C++11 scoped enums which don't implicitly convert to integral types. – James Adkison Jul 10 '17 at 17:42
-
@Allball103 What could a simple way to do it? Imagine to return an integral value from the function where `1` means solid, `2` gas and `3` liquid. `enum` is an improved version of this approach. – Davide Spataro Jul 10 '17 at 17:43
-
2@Allball103 If you *don't* look up things you don't know *you'll never get understand what you are doing*. Asking on StackOverflow should all ways be your *last* resort, not your first instinct. – Krupip Jul 10 '17 at 17:45
-
Thanks guys, that's a really simple solution. Just curious about downvotes: is there something fundamentally wrong with my question or is it just getting downvotes because it's dumb and has a simple solution? I'm not upset I'm just genuinely trying to improve my questions on here. – Allball103 Jul 10 '17 at 17:46
-
@snb dude if I'd known enums were a solution here I would've looked them up. I tried googling things similar to my question and came up short, I didn't realize there was an entire return type I was missing. – Allball103 Jul 10 '17 at 17:47
-
2I think you could do with (re)reading a C++ text book. – Ed Heal Jul 10 '17 at 17:47
-
@Allball103 I interpreted what you said earlier as "I didn't bother googling anything and opted to go on SO first". If that is not the case then I'm sorry. I am too also curious to why people are down-voting this question. – Krupip Jul 10 '17 at 17:49
-
@EdHeal isn't this website about learning things that you don't know? I've never taken a C++ class, I'm trying to teach myself. Sorry if that's a crime – Allball103 Jul 10 '17 at 17:50
-
@Allball103 - I think reading a text book on C++ will help you a lot - just to get an understanding of the basics – Ed Heal Jul 10 '17 at 17:53
-
@EdHeal except if you didn't go to school to learn c++, or didn't know of formal tutorials. I could see some one even forgetting Enum exist. This is not a bad question for people who don't know what they don't know – Krupip Jul 10 '17 at 17:53
-
@EdHeal definitely on my to-do list. Taking a class next semester. – Allball103 Jul 10 '17 at 17:54
-
@snb - I did not downvote btw. – Ed Heal Jul 10 '17 at 17:55
-
1@Allball103 - Please see https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list for a list of books – Ed Heal Jul 10 '17 at 17:56
-
THat list looks amazing, thanks – Allball103 Jul 10 '17 at 18:01
-
@Allball103 _"you're probably right but if i looked up every coding term I saw I'd never get to actually write programs"_ Strange approach. How do you expect to learn? How do you think we learnt? lol. You should indeed perform basic research before asking for our time – Lightness Races in Orbit Jul 10 '17 at 19:24
-
@LightnessRacesinOrbit I wish you'd read all the comments here: I DID do research before posting here. that quote was in reference to me saying that I'd heard the term enum before, but didn't know what it meant. Please do your research before wasting my time with your comment ;) – Allball103 Jul 10 '17 at 20:00
-
@Allball103: I did read them all. – Lightness Races in Orbit Jul 10 '17 at 20:02
1 Answers
3
Using an enum
would probably simplify your code and make it more readable. I'll provide you with this example.
class Thing {
enum MatterState {
SOLID,
LIQUID,
GAS
};
MatterState state;
// ... other variables
public:
// ... constructors, etc
MatterState get_state() {
return this->state;
}
};
You specified method, so I provided you with a class
.

Lightness Races in Orbit
- 378,754
- 76
- 643
- 1,055

Charles
- 1,384
- 11
- 18
-
1
-
@EdHeal OP specified _method_, not function. EDIT: I figured someone would ask that, so I preemptively edited my post (at the bottom). – Charles Jul 10 '17 at 17:50
-
-
I don't care enough to revert, but I had used the commented line numbers to make it easier to refer to certain code phrases. – Charles Jul 10 '17 at 19:31