strange namespace behaviour

C

CHRISTOF WARLICH

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
}
};
}
 
S

Sharad Kala

CHRISTOF WARLICH said:
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,

Because base class scope is searched before namespace b in name lookup. You
may want to check 3.4.1/7.

[Example:
namespace M {
class B { };
}
namespace N {
class Y : public M::B {
class X {
int a;
};
};
}

// The following scopes are searched for a declaration of i:
// 1) scope of class N::Y::X, before the use of i
// 2) scope of class N::Y, before the definition of N::Y::X
// 3) scope of N::Y's base class M::B
// 4) scope of namespace N, before the definition of N::Y
// 5) global scope, before the definition of N


---end example]

Sharad
 
C

CHRISTOF WARLICH

Sharad said:
Because base class scope is searched before namespace b in name lookup. You
may want to check 3.4.1/7.

Thanks a lot. This is interesting. But what are you referring to with 3.4.1/7?

Regards,

Christof
 
S

Sharad Kala

CHRISTOF WARLICH said:
Thanks a lot. This is interesting. But what are you referring to with
3.4.1/7?

I was referring to Section 3.4.1 paragraph 7 of the C++ Standard (ISO/IEC
14882:2003)

Sharad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top