Hi,
the following few lines of code are showing a quite
strange and unexpected behaviour of namespaces that
makes me worry wheater I should rely on namespaces in
the future at all.
The example below compiles if OK is defined, but gives
the following error otherwise:
$ g++ -c tst.cc
tst.cc: In method `void b::Y::h()':
tst.cc:21: cannot allocate an object of type `a::X'
tst.cc:21: since the following virtual functions are abstract:
tst.cc:5: void a::X::f()
I there anyone out there who is able to explain why
I need to explicitly specify namespace b in line 19?
For my understanding, line 21 should work as well,
because I am in namespace b already.
Thanks in advance for any help,
Christof
1 // #define OK
2 namespace a {
3 class X {
4 public:
5 virtual void f(void) = 0;
6 };
7 class Y: public X {
8 };
9 }
10 namespace b {
11 class X: public a::X {
12 public:
13 void f(void) {};
14 };
15 class Y: public a::Y {
16 public:
17 void h(void) {
18 #ifdef OK
19 X* x = new b::X();
20 #else
21 X* x = new X();
22 #endif
23 }
24 };
25 }
For (cut and paste) convenience, the same code again
without line numbers:
// #define OK
namespace a {
class X {
public:
virtual void f(void) = 0;
};
class Y: public X {
};
}
namespace b {
class X: public a::X {
public:
void f(void) {};
};
class Y: public a::Y {
public:
void h(void) {
#ifdef OK
X* x = new b::X();
#else
X* x = new X();
#endif
}
};
}