Take note of the following scenario:
class Packet {
public:
enum Opcode {
S2C_JOIN_GAME,
S2C_LEAVE_GAME
} opcode;
std::string payload;
};
class Client{
public:
void readPacket(Packet packet);
};
void Client::readPacket(Packet packet){
switch(packet.opcode){
case Packet::Opcode::S2C_JOIN_GAME:
//call some function to handle this case
break;
case Packet::Opcode::S2C_LEAVE_GAME:
//call another function to handle this case
break;
}
}
Within Client::readPacket, I need to check the opcode and call a specific function dependent on it. In my project I have a lot of different opcodes. Can I use a specific scope within my switch statement so I don't need to type Packet::Opcode every time?
For example:
void Client::readPacket(Packet packet){
switch(packet.opcode){
using namespace Packet::Opcode; //illegal, is there something similar?
using namespace Packet; // also illegal
case S2C_JOIN_GAME:
//do stuff.
break;
case S2C_LEAVE_GAME:
//do stuff.
break;
}
}
The code above will not compile because Packet is not a namespace. Is there an alternative way to get the same behavior in the example above without giving my enum type global scope?