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 7793355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:27:41+00:00 2026-06-01T22:27:41+00:00

a.h template <typename T> class A { public: int a; } b.h template <typename

  • 0

a.h

template <typename T>
class A
{
    public:
    int a;
}

b.h

template <typename T>
class B : public A<T>
{
   public:
   int f();
}

template <typename T>
int B<T>::f()
{
    int t;
    t = this->a; //Okay
    t = a //Error
    return 0;
}

why does error happen when I don’t use this->?

Can I omit this-> with using some method?

(I fixed some mistakes)

  • 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-06-01T22:27:43+00:00Added an answer on June 1, 2026 at 10:27 pm

    There are two phases in template instantiation (“Two Phase Name Lookup”).

    In the first phase, all non-dependent names are resolved (looked up). In the second phase, dependent names are resolved.

    A dependent name is a name which depends on a template parameter, e.g.:

    template <typename T>
    void foo() {
        x = 0;    // <- Non-dependent, nothing in that refers to "T".
                  //    Thus looked up in phase 1, therefore, an 'x' must be
                  //    visible.
    
        T::x = 0; // <- Dependent, because it depends on "T".
                  //    Looked up in phase 2, which is when it must be visible.
    }
    

    Now, you write:

    t = this->a; //Okay
    t = a //Error
    

    This is exactly what I described. In the okay term, t is looked up in phase 2,
    because this depends on a template parameter.

    The errorful term is looked up in phase 1, because nothing in that name depends on a template parameter.
    But in phase 1, no a is visible, because the compiler cannot introspect base class templates
    in phase 1, because templates can be specialized and at the point of instantiation,
    which can be remote from the primary template declaration, another specialization
    that has no a, might be visible.

    Example:

        template <typename T>
        struct Base {
        };
    
    
        template <typename T>
        struct Derived : Base<T> {
            void foo() {
                this->a = 0; // As is valid. `this->a` is looked up in phase 2.
            }
        };
    
    
        template <> struct Base<int> {
            int a;            
        };
    
    
        int main () 
        {
                // The following declarations trigger phase 2 lookup.
    
                Derived<int>   di;  // valid, because a later specialized
                                    // Base<int> is used and all symbols
                                    // are resolved.
    
                Derived<float> df;  // not valid
        }
    

    Btw, I have once written this-> is not only a matter of style in my very low frequency blog.

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

Sidebar

Related Questions

Why isn't this working: class/struct SomeClass { public: int SomeValue; } template <class/struct/typename T>
Consider this overly simple test: class foo { public: foo(int i); template< typename T
Try this out: template <typename T> class Base { public: int someBaseMember; }; template
This is my template matrix class: template<typename T> class Matrix { public: .... Matrix<T>
using namespace std; template<typename T> int f(vector<T> &v){ return v.size(); } template<typename T> class
I have template < typename threadFuncParamT > class ThreadWrapper { public: static int ThreadRoutineFunction(void*
I have something like this: class Base { public: static int Lolz() { return
In the file test.cpp, I have this: template <typename T> class A { public:
I have a class template <typename Iterator, typename Value> class Foo { public: Foo(const
template<typename T> class vec3 { public: typename T type_t; T x; T y; T

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.