Using the "using" keyword with classes

Hi,

I am having a problem with excessive scope qualifiers (::) such as this:

1
2
std::vector<Cube::Face> vecFaces = {Cube::f_RIGHT, Cube::f_TOP, Cube::f_RIGHT, Cube::f_TOP, Cube::f_RIGHT, Cube::f_TOP, Cube::f_TOP, Cube::f_RIGHT, Cube::f_TOP, Cube::f_TOP};
std::vector<Cube::Direction> vecDir = {Cube::d_UP, Cube::d_LEFT, Cube::d_DOWN, Cube::d_LEFT, Cube::d_UP, Cube::d_LEFT, Cube::d_LEFT, Cube::d_DOWN, Cube::d_LEFT, Cube::d_LEFT};

The std::vector is easy, it can become just vector by:

using namespace std;

or

using std::vector;

but I was wondering whether there is a way to do the same for Cube::. I have tried various arrangements of the keyword "using" with Cube, but the problem is that Cube is a class. Cube::Face and Cube::Direction are enums declared in Cube.

e.g.
1
2
3
4
5
class Cube
{
    enum Face {f_TOP, f_LEFT...}
    enum Direction {d_LEFT, d_UP...}
};

Is there some way to neaten up the first example code using typedefs or the using keyword (or any other way)?
Last edited on
If Cube is a namespace, you can just do the same as you would for std
1
2
3
4
5
6
7
8
9
namespace Cube
{
    enum Face {f_TOP, f_LEFT, r_TOP};
    enum Direction {d_LEFT, d_UP, d_RIGHT};
};

//in your code or main
using namespace Cube;
cout<<r_TOP<<d_RIGHT<<d_LEFT<<endl;
Hi ,
i am not aware of the method of initialization of the vector as


1
2
std::vector<Cube::Face> vecFaces = {Cube::f_RIGHT, Cube::f_TOP, Cube::f_RIGHT, Cube::f_TOP, Cube::f_RIGHT, Cube::f_TOP, Cube::f_TOP, Cube::f_RIGHT, Cube::f_TOP, Cube::f_TOP};
std::vector<Cube::Direction> vecDir = {Cube::d_UP, Cube::d_LEFT, Cube::d_DOWN, Cube::d_LEFT, Cube::d_UP, Cube::d_LEFT, Cube::d_LEFT, Cube::d_DOWN, Cube::d_LEFT, Cube::d_LEFT};


please correct me if i am wrong ...
Last edited on
@bluecoder it's a C++11 feature. Try it on ideone.com: http://ideone.com/0A4iO
@therockon7throw
The problem is that Cube is a class not a namespace
You can use a typedef I believe:

typedef Cube::Face NewFace;
Would I then have to typedef every element of the enum?
I.e.

1
2
3
4
5
typedef Cube::Face        NewFace;
typedef Cube::f_RIGHT     NewRIGHT;
typedef Cube::f_LEFT      NewLEFT;
typedef Cube::f_TOP       NewTOP;
etc.
Is there a possible easier way?
What's wrong with typing Cube::Face? Too much, too lazy?

To initialize the non C++11 version:

const Cube::Face Faces[] = {Cube::f_RIGHT, Cube::f_TOP, Cube::f_RIGHT, Cube::f_TOP, Cube::f_RIGHT, Cube::f_TOP, Cube::f_TOP, Cube::f_RIGHT, Cube::f_TOP, Cube::f_TOP};

std::vector<Cube::Face> vecFaces(Faces, Faces + (sizeof(Faces) / sizeof(*Faces)));
or later
vecFaces.assign(Faces, Faces + (sizeof(Faces) / sizeof(*Faces)));

You can easily use such a constant array with each STL container.
What's wrong with typing Cube::Face? Too much, too lazy?
1
2
3
#define Fc Cube::Face
// Use Fc instead of Cube::Face now.
#undef Fc 
closed account (zb0S216C)
You cannot use the scope resolution operator on a enumeration. This is because enumerations are not scoped types. Some compilers will allow this behaviour, but only as a non-standard extension. However, in C++11, enum class was introduced which allows the scoping of enumerators, as well as the ability to choose the underlying type of the enumerators.

An alternative to this is using the namespace - therockon7throw has already suggested this. However, I don't recommend uses classes, because namespaces are more suited for the job.

Wazzak
Last edited on
Topic archived. No new replies allowed.