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
- Type Alias: Create an alias for a type, similar to
typedef. - Namespace Alias: Shorten long namespace names for convenience.
- Import Names from Namespace: Bring specific names from a namespace into the current scope.
- 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:
- The line
using IntVector = vector<int>;creates a type aliasIntVectorforvector<int>. - Instead of writing
vector<int>, the aliasIntVectorcan be used to declare and manipulate integer vectors. - 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:
- The namespace
LongNamespaceNamecontains a functiondisplay(). - An alias
LNNis created forLongNamespaceNameusingusing LNN = LongNamespaceName;. - The function
display()is accessed using the aliasLNN, 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:
- The
usingstatement imports specific namescoutandendlfrom thestdnamespace into the current scope. - 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:
- The class
Basehas a protected member functiongreet(). - In the derived class
Derived, theusingkeyword is used to makegreet()accessible in the public interface ofDerived. - The function
greet()is called using an object ofDerived, demonstrating howusingsimplifies access to base class members.
Key Points about using Keyword
- The
usingkeyword is versatile and can be used for type aliases, namespace aliases, and importing specific names from namespaces. - It simplifies code by reducing verbosity, especially when dealing with long type or namespace names.
- When used in derived classes,
usingallows access to specific base class members. - Unlike
typedef,usingworks with templates, making it more flexible for type aliases.
