Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Calling virtual method of base class nested in template class
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Roger  
View profile   Translate to Translated (View Original)
 More options 28 Aug 2006, 23:06
Newsgroups: comp.lang.c++.moderated
From: "Roger" <some...@idontwantspam.com>
Date: 28 Aug 2006 18:06:06 -0400
Local: Mon 28 Aug 2006 23:06
Subject: Calling virtual method of base class nested in template class
Hi all.

Here's a little quandry I've run in to and can't tell if it's a compiler
bug, or if it's bad code.  First, here's the code:

===================================
#include <stdio.h>

template < typename T >
class B
{

public:

  B( ) { }

  class N1
  {

  public:

    N1( ) { };

    class N2
    {

    public:

      N2( ) { };

      virtual void f( )
      {
        printf( " B< >::N1::N2::f( )\n" );
      }

    };

  };

  T _m;

};

class D : public B< int >
{

public:

  class N1 : public B< int >::N1
  {

  public:

    N1( ) : B< int >::N1( ) { };

    class N2 : public B< int >::N1::N2
    {

    public:

      N2( ) : B< int >::N1::N2( ) { };

      virtual void f( )
      {
        printf( " D::N1::N2::f( )\n" );
        ( ( B< int >::N1::N2* ) this )->f( );
      }

    };

  };

};

void
main( int argc, char* argv[ ] )
{
  D::N1::N2 n2;

  n2.f( );

}

==================================

Invoking n2.f( ) causes an infinite loop, dumping out "D::N1::N2::f( )"
ad-nausium.  I am trying to call the base-class f( ) in B::N1::N2, but the
compiler just generates code that instead calls D::N1::N2::f( ) recursively.

What am I doning wrong?  How can I call the base's f( )?

Thanks for any help you can offer!

- Roger

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google