Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3407050
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:42:12+00:00 2026-05-18T05:42:12+00:00

I have code like this: namespace N { class B { public: virtual void

  • 0

I have code like this:

namespace N {
   class B {
     public:
       virtual void doStuff(B *) = 0;
    };
 }

 // not in a namespace
 class Derived : public N::B {
   public:
       void doStuff(B *); // Should this be N::B, or is B ok?
 };

Do I need the namespace qualifier where Derived refers to it’s base class? GCC and MSVC are happy with the code as written, but another compiler complains unless I put the namespace in. What does the C++ standard say?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:42:13+00:00Added an answer on May 18, 2026 at 5:42 am

    Inside the class definition B is OK. That’s the so-called injected class name.

    This also refers to templates (not counting dependent bases). E.g.

    template <class T> class B{};
    template <class T> class C: public B<int>
    {
       void f(B* p) {} //same as B<int>* p
       void f(C* p) {} //same as C<T>* p
    };
    

    In general the base class (and the class itself) can be referred to inside the class definition without qualification or template arguments.

    Quotes from the standard:

    9.2 : A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also
    inserted into the scope of the class itself; this is known as the
    injected-class-name. For purposes of access checking, the
    injected-class-name is treated as if it were a public member name.

    From this definition it follows that the name of the class itself is publicly accessible from the class, and therefore is available in derived classes. Which proves my point about B being OK along with N::B because the name B is inherited

    Btw, this also explains why the following is invalid:

    template <class T> class B{};
    template <class T> class C: public B<T>
    {
         void f(B* p){} //ERROR
        // the above is invalid!! Base class is dependent therefore 
        //its scope is not considered during unqualified name lookup
         void g(typename C::B* p){} //this is valid, same as B<T>* p  
    };
    

    14.6.1 Speaks about injected class names in templates. It is far too long to paste here.
    Hth

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure that represents C# code like this: class Namespace: string
Suppose I have code like this: template<class T, T initial_t> class Bar { //
If I have code like this: public XALServiceConfiguration CreateInstance() { var config = ConfigurationManager.GetSection(ConfigurationSectionName)
I need to move backwards through an array, so I have code like this:
Shape.h namespace Graphics { class Shape { public: virtual void Render(Point point) {}; };
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this: template <typename T, typename U> struct MyStruct { T
I have code like this to move the player in my game left, right,
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I'm using PHP5 to create XML files. I have code like this: $doc =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.