Can't access variable in template base class

RiaD

I want to access protected variable in parent class, I have the following code and it compiles fine:

class Base
{
protected:
    int a;
};

class Child : protected Base
{
public:
    int b;
    void foo(){
        b = a;
    }
};

int main() {
    Child c;
    c.foo();
}

Ok, now I want to make everything templated. I changed code to the following

template<typename T>
class Base
{
protected:
    int a;
};

template <typename T>
class Child : protected Base<T>
{
public:
    int b;
    void foo(){
        b = a;
    }
};

int main() {
    Child<int> c;
    c.foo();
}

And got error:

test.cpp: In member function ‘void Child<T>::foo()’:
test.cpp:14:17: error: ‘a’ was not declared in this scope
             b = a;
                 ^

Is it correct behavior? What's the difference?

I use g++ 4.9.1

Lightness Races in Orbit

Hehe, my favourite C++ oddity!

This will work:

void foo()
{
   b = this->a;
//     ^^^^^^
}

Unqualified lookup doesn't work here because the base is a template. That's just the way it is, and comes down to highly technical details about how C++ programs are translated.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can interface in java access to base class variable?

From Dev

Can't access variable inside same class

From Dev

Can't access class variable in ruby/rails?

From Dev

Can't access variable within class instance

From Dev

Can't access variable from different class

From Dev

Why method can't access class variable?

From Dev

Derived class can't access protected method of base class

From Dev

I can't find a way to access a variable in my html template

From Dev

C# Can't access base properties of a class (only inherited)

From Dev

C# Can't access base properties of a class (only inherited)

From Dev

Access attribute of template base class without "using"

From Dev

A method can't access a member variable of the same class (C++)

From Dev

Can't access class instance variable set in another module

From Dev

why can't I access the variable in my class. python

From Dev

Why I can't access my script variable from class?

From Dev

Can't Access Instance Variable Inside Method of same Class?

From Dev

Can't access a variable from another class in Java?

From Dev

Why `this` can't access the derived class members from base class methods when called for derived class object

From Dev

C++ public method inherited from base class can not access private member variable in derived class

From Dev

does inherited class can access base class

From Dev

Template Class derived from Non-template base class: No access to base class variables?

From Java

Can't access properties of the base class of the class I am using as the type argument in a generic method

From Dev

Why a derived class can't access a protected getter from the base class?

From Dev

Why an instance of inherited class can't access to protected member of base class in different package

From Dev

Can't access derived class method from pointer of type base class

From Dev

Why an instance of inherited class can't access to protected member of base class in different package

From Dev

Why can't I call a template base class constructor with a const_iterator?

From Dev

Why can't I call a template base class constructor with a const_iterator?

From Dev

Can't access variable in a function

Related Related

  1. 1

    Can interface in java access to base class variable?

  2. 2

    Can't access variable inside same class

  3. 3

    Can't access class variable in ruby/rails?

  4. 4

    Can't access variable within class instance

  5. 5

    Can't access variable from different class

  6. 6

    Why method can't access class variable?

  7. 7

    Derived class can't access protected method of base class

  8. 8

    I can't find a way to access a variable in my html template

  9. 9

    C# Can't access base properties of a class (only inherited)

  10. 10

    C# Can't access base properties of a class (only inherited)

  11. 11

    Access attribute of template base class without "using"

  12. 12

    A method can't access a member variable of the same class (C++)

  13. 13

    Can't access class instance variable set in another module

  14. 14

    why can't I access the variable in my class. python

  15. 15

    Why I can't access my script variable from class?

  16. 16

    Can't Access Instance Variable Inside Method of same Class?

  17. 17

    Can't access a variable from another class in Java?

  18. 18

    Why `this` can't access the derived class members from base class methods when called for derived class object

  19. 19

    C++ public method inherited from base class can not access private member variable in derived class

  20. 20

    does inherited class can access base class

  21. 21

    Template Class derived from Non-template base class: No access to base class variables?

  22. 22

    Can't access properties of the base class of the class I am using as the type argument in a generic method

  23. 23

    Why a derived class can't access a protected getter from the base class?

  24. 24

    Why an instance of inherited class can't access to protected member of base class in different package

  25. 25

    Can't access derived class method from pointer of type base class

  26. 26

    Why an instance of inherited class can't access to protected member of base class in different package

  27. 27

    Why can't I call a template base class constructor with a const_iterator?

  28. 28

    Why can't I call a template base class constructor with a const_iterator?

  29. 29

    Can't access variable in a function

HotTag

Archive