C++ using Keyword

The using keyword in C++ has multiple purposes, depending on the context in which it is used. It is commonly used to create type aliases, import names from namespaces, and simplify access to base class members in derived classes.

The using keyword enhances readability and simplifies code by providing flexible ways to handle names and types.


Uses of the using Keyword

  1. Type Alias: Create an alias for a type, similar to typedef.
  2. Namespace Alias: Shorten long namespace names for convenience.
  3. Import Names from Namespace: Bring specific names from a namespace into the current scope.
  4. Access Base Class Members: Inherited members from a base class can be made accessible in a derived class.

Syntax

1. Type Alias

</>
Copy
using alias_name = type;

2. Namespace Alias

</>
Copy
using alias_name = namespace_name;

3. Import Names from Namespace

</>
Copy
using namespace_name::name;

4. Access Base Class Members

</>
Copy
using BaseClass::member_name;

Examples for “using” keyword

Example 1: Using for Type Alias

This example demonstrates how to create a type alias using the using keyword.

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

using IntVector = vector<int>; // Type alias for vector

int main() {
    IntVector numbers = {1, 2, 3, 4, 5};
    for (int num : numbers) {
        cout << num << " ";
    }
    return 0;
}

Output:

1 2 3 4 5

Explanation:

  1. The line using IntVector = vector<int>; creates a type alias IntVector for vector<int>.
  2. Instead of writing vector<int>, the alias IntVector can be used to declare and manipulate integer vectors.
  3. A vector of integers is created, initialized, and iterated over using a range-based for loop.

Example 2: Using for Namespace Alias

This example demonstrates how to create an alias for a namespace to simplify code.

</>
Copy
#include <iostream>
namespace LongNamespaceName {
    void display() {
        std::cout << "Hello from LongNamespaceName!" << std::endl;
    }
}

using LNN = LongNamespaceName; // Namespace alias

int main() {
    LNN::display(); // Access function using alias
    return 0;
}

Output:

Hello from LongNamespaceName!

Explanation:

  1. The namespace LongNamespaceName contains a function display().
  2. An alias LNN is created for LongNamespaceName using using LNN = LongNamespaceName;.
  3. The function display() is accessed using the alias LNN, reducing verbosity.

Example 3: Importing Names from a Namespace

This example demonstrates importing a specific name from a namespace into the current scope.

</>
Copy
#include <iostream>
using std::cout;
using std::endl;

int main() {
    cout << "Hello, World!" << endl; // No need for std:: prefix
    return 0;
}

Output:

Hello, World!

Explanation:

  1. The using statement imports specific names cout and endl from the std namespace into the current scope.
  2. Once imported, these names can be used without the std:: prefix, simplifying the code.

Example 4: Accessing Base Class Members

This example demonstrates how to use the using keyword to make base class members accessible in a derived class.

</>
Copy
#include <iostream>
using namespace std;

class Base {
protected:
    void greet() {
        cout << "Hello from Base!" << endl;
    }
};

class Derived : public Base {
public:
    using Base::greet; // Make Base's greet function accessible
};

int main() {
    Derived obj;
    obj.greet(); // Access Base class's greet function
    return 0;
}

Output:

Hello from Base!

Explanation:

  1. The class Base has a protected member function greet().
  2. In the derived class Derived, the using keyword is used to make greet() accessible in the public interface of Derived.
  3. The function greet() is called using an object of Derived, demonstrating how using simplifies access to base class members.

Key Points about using Keyword

  1. The using keyword is versatile and can be used for type aliases, namespace aliases, and importing specific names from namespaces.
  2. It simplifies code by reducing verbosity, especially when dealing with long type or namespace names.
  3. When used in derived classes, using allows access to specific base class members.
  4. Unlike typedef, using works with templates, making it more flexible for type aliases.