Namespace inheritance
First of all I cannot understand why i get this error
error: 'cout' was not declared in this scope
For the following code
#include <iostream>
namespace a {
void print() {
std:cout << "a" << std::endl;
}
}
namespace b {
using namespace a;
void print() {
std:cout << "b" << std::endl;
}
}
int main() {
b::print();
}
Broadly speaking I'm investigating the behavior of inheritance in
namespaces since I don't need classes per se (they would just be
singletons anyway). Any advice or gotchas to look out for with the
namespaces approach?
Edit: Okay I;m sorry about the typo. In that case can i extend the question?
#include <iostream>
namespace a {
void print2() {
print();
}
void print() {
std::cout << "a" << std::endl;
}
}
namespace b {
using namespace a;
void print() {
std::cout << "b" << std::endl;
}
}
int main() {
b::print2();
}
This doesn't compile error: 'print' was not declared in this scope
Is there a way i can emulate class inheritance in a generic way?
For example if a and b were classes, a would be printed. If print was
declared as virtual then b would be printed.
Is any of this possible or is namespace "inheritance" just a cheap hack?
No comments:
Post a Comment