In the code snipped below ns2::my_class::foo
member function has to explicitly take ns1::my_object
, which is fine however, it is somewhat cumbersome when there are a lot of such places. The need to use the namespace arises from the use of the same name for the classes in different namespaces. I know that that’s basically what the namespaces are for but I’m wondering if there is a way to improve the design other than using prefixes?
#include <iostream>
using namespace std;
namespace ns1 {
struct my_object {};
struct my_class {
virtual void foo(my_object *object) {}
};
}
namespace ns2 {
struct my_object {};
struct my_class : public ns1::my_class {
void foo(my_object *object) override {} // compile error
void foo(ns1::my_object *object) override {} // OK
};
}
int main() {
return 0;
}