using
The using
keyword in C++ serves multiple purposes, primarily related to name resolution and type aliasing. It's a versatile keyword that can be used for creating aliases, bringing names into scope, and working with inheritance.
typedef
)#include <iostream>
#include <vector>
int main() {
using IntVector = std::vector<int>;
IntVector numbers = {1, 2, 3, 4, 5};
for (const auto& num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
using IntVector = std::vector<int>;
creates an alias for std::vector<int>
.using
are more flexible than typedef
, especially with templates.#include <iostream>
namespace Math {
const double PI = 3.14159;
double square(double x) { return x * x; }
}
int main() {
using Math::PI;
using Math::square;
std::cout << "Pi: " << PI << std::endl;
std::cout << "5 squared: " << square(5) << std::endl;
return 0;
}
using Math::PI;
and using Math::square;
bring specific names from the Math
namespace into the current scope.Math::
qualifier.#include <iostream>
#include <string>
class Animal {
public:
Animal(const std::string& name) : name_(name) {}
void speak() const { std::cout << "Some animal sound" << std::endl; }
protected:
std::string name_;
};
class Dog : public Animal {
public:
using Animal::Animal; // Inherit constructors
void speak() const { std::cout << name_ << " says: Woof!" << std::endl; }
};
int main() {
Dog dog("Buddy");
dog.speak();
return 0;
}
using Animal::Animal;
in the Dog
class inherits all constructors from the Animal
base class.#include <iostream>
#include <vector>
#include <algorithm>
int main() {
using namespace std;
vector<int> numbers = {5, 2, 8, 1, 9};
sort(numbers.begin(), numbers.end());
for (const auto& num : numbers) {
cout << num << " ";
}
cout << endl;
return 0;
}
using namespace std;
brings all names from the std
namespace into the current scope.Scope: Using-declarations and type aliases with using
are subject to the usual scope rules in C++.
Best Practices: Prefer using-declarations over using-directives to minimize potential name conflicts.
Headers: Avoid using-directives in header files as they can lead to unexpected name resolution in including files.
Readability: While using
can improve readability, overuse can lead to confusion about where names are coming from.
The using
keyword in C++ is a versatile tool for name management and type aliasing. It can be used to create type aliases, bring specific names or entire namespaces into scope, and inherit constructors in derived classes. While powerful, it should be used judiciously, especially in the case of using-directives. Type aliases and using-declarations for specific names are generally safe and can improve code readability. The inheritance of constructors with using
can simplify class hierarchies. However, care should be taken to avoid name conflicts, particularly when working with large codebases or writing header files. Understanding the various applications of using
allows C++ programmers to write more concise and maintainable code while managing namespaces effectively.
[1] https://en.cppreference.com/w/cpp/keyword/using [2] https://stackoverflow.com/questions/48356856/using-directive-vs-using-declaration [3] https://quuxplusone.github.io/blog/2020/12/21/using-directive/ [4] https://cplusplus.com/forum/general/250849/ [5] https://www.learncpp.com/cpp-tutorial/using-declarations-and-using-directives/
Previous Page | Course Schedule | Course Content