473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

strange namespace behaviour

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
3 1232

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: brian.bird | last post by:
Can anyone explain the behaviour of python when running this script? >>> def method(n, bits=): .... bits.append(n) .... print bits .... >>> method(1) >>> method(2)
3
by: Sebastian C. | last post by:
Hello everybody Since I upgraded my Office XP Professional to SP3 I got strange behaviour. Pieces of code which works for 3 years now are suddenly stop to work properly. I have Office XP...
1
by: Antonio | last post by:
Hy Jon, Still some problem, because I want to use also the chinese translation for the page http://www.etantonio.it/EN/Economia/indexTest.aspx whose code is the following : ...
1
by: Jitesh Sinha | last post by:
Hi, I am running Windows 2003/ IIS 6.0. I was stuck with rather a abnormal behaviour of System.Web.mail class. It was truncating the message body after 3,071 character. The code i was testing...
19
by: Larry Smith | last post by:
Hi there, When I run the following on my app's primary thread: System.Type.GetType("System.Windows.Forms.Form") It works as expected. If I then launch a thread via the "BackgroundWorker"...
8
by: Paulo da Silva | last post by:
Hi! Why doesn't this work? If I change the name of the vector toLower for ex. to toLowerV it works! (GCC) Thanks. Paulo ..h _______________
5
by: =?Utf-8?B?Y2FsZGVyYXJh?= | last post by:
Dear all, I have a strange behaviour on the use of delagate. I will try to explain. I have an assembly (Lets call it AssmDB) which contains database function operation like Insert, Delete,...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
8
by: Bo Yang | last post by:
Hi, Today, I make some test on the C++ STL iterators of set containers with GNU g++ compiler. The code is: #include <set> #include <iostream> using namespace std; int main(int argc, char...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.