26

Can we alias a class name the way we do in namespaces?

For example:

namespace longname{ }
namespace ln = longname;// namespace aliasing

class LONGNAME {};
class LN = LONGNAME; // how to do class name aliasing, if allowed?
1
  • 5
    typedef should do it, unless I'm misunderstanding?
    – Carl Norum
    Mar 20, 2012 at 16:50

4 Answers 4

34

Simple:

typedef LONGNAME LN;

Typedefs are used in C++ a bit like "variables which can store types". Example:

class Car
{
public:
    typedef std::vector<Wheel> WheelCollection;

    WheelCollection wheels;
};

By using Car::WheelCollection everywhere instead of std::vector<Wheel>, you can change the container type in one place and have all your code reflect the changes. This is the C++ way to abstract data types (whereas eg. in C# you'd have a property returning IEnumerable<Wheel>).

4
  • 1
    When you posted that as an answer and not comment you should have shown a little more courtesy to explain at least the basics. Mar 21, 2012 at 5:04
  • @Rohit: What basics ? Typedef is the simplest statement of C++, and there is nothing to know except having seen it once. Mar 21, 2012 at 7:29
  • A little theory on how to use it. or what is a typedef, etc would be great answer Mar 21, 2012 at 7:42
  • 1
    @Rohit: I added a typical use case. Mar 21, 2012 at 8:02
20

Beside the answers already provided using the keyword typedef, you also can use the keyword using since C++11. IMHO it looks more consistent regarding the aliasing.

namespace longname{ }
namespace ln = longname;// namespace aliasing

class LONGNAME {};
using LN = LONGNAME; // "class aliasing"

In addition, with using you are able to alias template classes (How to typedef a template class?) by using alias templates.

template<typename T> class LONGNAME {};
template<typename T> using LN = LONGNAME<T>; // alias template
2
  • 2
    Effective modern C++ 2014, S. Meyers, Item 9 Jun 27, 2018 at 13:22
  • This is the right answer for c++11 on-words
    – Vassilis
    Apr 13, 2022 at 6:49
6

You can use the typedef keyword:

typedef LONGNAME LN;

You can also do something like:

typedef class {

...

} LN;

Edit: You may run into trouble when using templates though. See here for a possible solution.

2
  • 1
    The provided link is unrelated, as it deals with templates, not classes, and those are completely different beasts. Mar 20, 2012 at 18:39
  • @DavidRodríguez-dribeas I just thought the OP also might be interesting in typedefing template classes. Mar 21, 2012 at 4:56
1
typedef int mark;  // for in built data types

class abc
{
};

typedef abc XYZ; // for user written classes.

Typedef allows you to alias a class or datatype name with a more context sensitive name corresponding to the scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.