In C++, heterogeneous lookup allows us to access members of a class that can be found in types of different categories, such as base classes. This can be particularly useful when working with standard containers like `std::queue`.
Below is an example that demonstrates how to leverage heterogeneous lookup with `std::queue`. In this example, we have a base class `Base` and a derived class `Derived`. We create a queue of `Derived` objects and access members from both classes.
#include
#include
class Base {
public:
void display() {
std::cout << "I am Base class" << std::endl;
}
};
class Derived : public Base {
public:
void show() {
std::cout << "I am Derived class" << std::endl;
}
};
int main() {
std::queue myQueue;
Derived d1;
myQueue.push(d1);
// Accessing Derived class member
myQueue.front().show();
// Accessing Base class member
myQueue.front().display();
return 0;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?