Lorenzo Pistone
Lorenzo Pistone

Reputation: 5188

clash between class name and enum value: resolvable without namespaces?

class cippa{};

enum close{ cippa };

int main(){
    new cippa();    //bad here
}

Using ::cippa doesn't help either. Is there a way to solve this without putting either the enum or the class in a separate namespace?

Upvotes: 0

Views: 394

Answers (2)

Barney Szabolcs
Barney Szabolcs

Reputation: 12514

With C++11, if you do

class cippa;
enum class close { cippa };

then class cippa and and enum value close::cippa will not clash.

By the way that is essentially doing

class close{
public:
  enum enum_t{cippa};
};

But then instead of close you need to use close::enum_t to access the enum type. close::cippa remains the same.

Upvotes: 1

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506905

Disambiguate using new class cippa. If a class name and enumerator (or function/variable) name is declared in the same scope, the class name is hidden. You can access it by class name. Same if the type name is an enumeration name. You could access that by enum name

#include <unistd.h>

// oops, close is now hidden! but we know a trick..
enum close c = cippa;

Upvotes: 4

Related Questions