By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,166 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,166 IT Pros & Developers. It's quick & easy.

strange namespace behaviour

P: n/a
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
}
};
}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"CHRISTOF WARLICH" <cw******@lucent.com> wrote in message > 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,


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[i];
};
};
}

// 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

Jul 22 '05 #2

P: n/a
Sharad Kala wrote:
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
Jul 22 '05 #3

P: n/a

"CHRISTOF WARLICH" <cw******@lucent.com> wrote in message
news:41***************@lucent.com...
Sharad Kala wrote:
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?

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

Sharad
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.