What does ‘using’ mean in C++?
In C++, the keyword “using” has two different usages and meanings.
- using is used to declare an alias:
- When we want to give a new alias to an existing type or namespace, we can use the using keyword. For example, using MyInt = int; and using namespace std; in the above code rename the int type to MyInt and introduce the std namespace into the current scope, allowing us to use its definitions directly without needing to add the namespace prefix.
- employ for introducing member functions of a base class:
- When a derived class inherits from a base class, if you want to directly use a member function of the base class in the derived class, you can use the using keyword. For example:
class Base {
public:
void print() {
cout << "Base class" << endl; } }; class Derived : public Base { public: using Base::print; }; In the above code, using Base::print; imports the print() function from the base class Base into the derived class Derived, allowing us to directly call the print() function in the Derived class without needing to rewrite a new function.
Note: In standards before C++11, the keyword “using” could only be used to introduce member functions from a base class, but in the C++11 standard, “using” alias was also introduced for declaring type aliases.