strange namespace behaviour

Discussion in 'C++' started by CHRISTOF WARLICH, Sep 17, 2004.

  1. 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
    }
    };
    }
     
    CHRISTOF WARLICH, Sep 17, 2004
    #1
    1. Advertising

  2. CHRISTOF WARLICH

    Sharad Kala Guest

    "CHRISTOF WARLICH" <> 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;
    };
    };
    }

    // 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
     
    Sharad Kala, Sep 17, 2004
    #2
    1. Advertising

  3. 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
     
    CHRISTOF WARLICH, Sep 17, 2004
    #3
  4. CHRISTOF WARLICH

    Sharad Kala Guest

    "CHRISTOF WARLICH" <> wrote in message
    news:...
    > 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
     
    Sharad Kala, Sep 17, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    585
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,146
  3. Anonymous
    Replies:
    3
    Views:
    542
    Ron Natalie
    Aug 18, 2003
  4. Replies:
    4
    Views:
    355
    Old Wolf
    Nov 16, 2005
  5. Andy Chambers
    Replies:
    1
    Views:
    395
    Daniel Dyer
    May 14, 2007
Loading...

Share This Page