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 aliasIntVector
forvector<int>
. - Instead of writing
vector<int>
, the aliasIntVector
can 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
LongNamespaceName
contains a functiondisplay()
. - An alias
LNN
is created forLongNamespaceName
usingusing 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
using
statement imports specific namescout
andendl
from thestd
namespace 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
Base
has a protected member functiongreet()
. - In the derived class
Derived
, theusing
keyword is used to makegreet()
accessible in the public interface ofDerived
. - The function
greet()
is called using an object ofDerived
, demonstrating howusing
simplifies access to base class members.
Key Points about using
Keyword
- The
using
keyword 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,
using
allows access to specific base class members. - Unlike
typedef
,using
works with templates, making it more flexible for type aliases.