Quick Question Quick Answer

J

JKop

Take the following:


enum Sense
{
Vision,
Hearing,
Touch,
Smell
};


void Jaguar(const Sense& sense)
{
return;
}


int main(void)
{
Sense sense = Hearing;

Jaguar(sense);

Jaguar(Hearing);



int Hearing;

Jaguar(Sense::Hearing); //ERROR `Sense' is not an aggregate type

return 0;
}



How do I tell it that I'm referring to "Hearing" from the enum "Sense", and
not the local variable entitled "Hearing"?!


-JKop
 
J

Jorge Rivera

How do I tell it that I'm referring to "Hearing" from the enum
"Sense", and
not the local variable entitled "Hearing"?!

I think you just hit a fundamental limitation of enum types in C++.

enums are inherently integer values, and as you point out, there is no
real way to make this distinction.

One possible solution is to enclose your enum in a namespace.

Eg.

namespace Sense{
enum SenseType
{
Vision, Hearing, Touch, Smell
};
}

void Jaguar(const Sense::SenseType& sense);

now you function can be called using

Jaguar(Sense::Hearing);


JLR
 
S

Stephen Waits

JKop said:
How do I tell it that I'm referring to "Hearing" from the enum "Sense", and
not the local variable entitled "Hearing"?!

Don't do this for the same reason you don't do this:

int blah()
{
float a;
int a;

...
}
 
J

JKop

It's about time I got a real in-depth C++ book, or maybe read the standard a
little bit.

Anyway, here's what I've realized... By writing:


enum Water
{
chocolate,
monkey,
orange,
five,
};


you *are* in fact declaring global const variables and you *are* using up
token-names. For instance, the following will fail to compile:


enum Water
{
chocolate,
monkey,
orange,
five,
};

enum Oil
{
chocolate,
monkey,
orange,
five
};


Multiple definition.


Moving on, here's a nice bit of code:



namespace Water {
enum Water
{
chocolate,
monkey,
orange,
five,
}; }

namespace Oil {
enum Oil
{
chocolate,
monkey,
orange,
five
}; }




void WaterPP(const Water::Water& pp)
{
return;
}



void OilSS(const Oil::Oil& ss)
{
return;
}



int main(void)
{
Water::Water Watergg;

Oil::Oil Oilkk;


WaterPP(Watergg);

WaterPP(Water::chocolate);

//WaterPP(chocolate); //ERROR undeclared


OilSS(Oilkk);

OilSS(Oil::chocolate);

//OilSS(chocolate); //ERROR undeclared


int chocolate; //This own't interfer!

}



Namespaces, gotta love them.


-JKop
 
J

Julie

JKop said:
namespace Water {
enum Water
{
chocolate,
monkey,
orange,
five,
}; }
...
void WaterPP(const Water::Water& pp)
{
return;
}

You may want to consider a different name for the enum to differentiate it from
the namespace so that you don't have things like Water::Water, etc. Some other
naming choices include Type, Value, etc. With naming like that, you can then
use Water::Type& pp which may be a little easier to follow/read.
 
S

Stephen Waits

JKop said:
Anyway, here's what I've realized... By writing:

enum Water
{
chocolate,
monkey,
orange,
five,
};

you *are* in fact declaring global const variables and you *are* using up
token-names. For instance, the following will fail to compile:

Nope.

--Steve
 
A

Alan Johnson

JKop said:
Stephen Waits posted:





Now that's just stupid. Express yourself, elborate.


-JKop

I assume what Stephen is taking exception to with his cryptically worded
"Nope." is the fact that you are calling them variables, which they are
not. And here is why:

const int a = 0 ; // Like any variable, this allocates memory.
const int *pa = &a ; // We can get a pointer to the memory.


In constrast,

enum some_name { a } ; // This doesn't allocate memory.
const int *pa = &a ; // This won't work.


Alan
 
J

JKop

Alan Johnson posted:
I assume what Stephen is taking exception to with his cryptically worded
"Nope." is the fact that you are calling them variables, which they are
not. And here is why:

const int a = 0 ; // Like any variable, this allocates memory.
const int *pa = &a ; // We can get a pointer to the memory.


In constrast,

enum some_name { a } ; // This doesn't allocate memory.
const int *pa = &a ; // This won't work.


Alan


Alan you are absolutely correct.


But having a human mind, I assume that you "got the jist" of what I was
getting at.


-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top