Andrea993 February 2016

Abbreviate the full name of a type defined in a class

Which is the best way to avoid to write the class name when I use a type defined in a class?

For example I have a class MyClass where is defined a mytype type, out of this class I need use mytype and I must write:

MyClass::mytype foo

I would like to write only

mytype foo

I've try with

#define MyClass::mytype mytype

It works but I think that this is not a good solution. What should I use?

Answers


Claudiu February 2016

You can do:

typedef MyClass::mytype mytype;


Brian February 2016

Use a typedef or an alias declaration, ideally in the narrowest possible scope.

void some_function() {
    typedef MyClass::mytype mytype;
    // or: using mytype = MyClass::mytype;
}
// namespace not polluted

Post Status

Asked in February 2016
Viewed 1,100 times
Voted 12
Answered 2 times

Search




Leave an answer