Possible stupid switch statement q

Hey All,

I have a possibly silly switch-statement q:

Why doesn’t something like this work:

bool MyComponent::keyPressed(const KeyPress& key) {
	switch (key.getKeyCode()) {
		case KeyPress::deleteKey:

The compiler complains like this:
error: ‘juce::KeyPress::deleteKey’ cannot appear in a constant-expression

Using gcc 4 in OS X 10.5

I thought that juce::KeyPress::deleteKey was const, and therefore kosher…

Anyways, clarification would be nice. I come from Java, so some of the C++ arcana is still a bit mysterious to me.

thanks,

c.

try.

if(key.isKeyCode(KeyPress::deleteKey)) { //deleteKey stuff... } else if(key.isKeyCode(KeyPress::escapeKey)) { //escapeKey stuff... } else if(key.isKeyCode(KeyPress::backspaceKey)) { //backspaceKey stuff... }

yes yes, that works, but that is no fun. god invented switch statements for a reason :slight_smile:

i really want to know why the switch won’t work… not minding that it is actually more efficient, but it also seems like less typing…

c.

It’s because the initializer is in the declaration, not the definition, it doesn’t qualify as a constant integral expression.

If it was like this, it would work:

class Foo
{
public:
static const int a = 1;
static const int b = 2;
static const int c = 3;
};

int main()
{
int v = 2;
switch (v)
{
case Foo::a: break;
case Foo::b: break;
case Foo::c: break;
}
}

const int Foo::a;
const int Foo::b;
const int Foo::c;

But since it is like this, it doesn’t:

class Foo
{
public:
static const int a;
static const int b;
static const int c;
};

int main()
{
int v = 2;
switch (v)
{
case Foo::a: break;
case Foo::b: break;
case Foo::c: break;
}
}

const int Foo::a = 1;
const int Foo::b = 2;
const int Foo::c = 3;

ah, thanks. That is good to know.

c.