thewoz
thewoz

Reputation: 562

Access to a member data via a class template specialisation

I can not access the member data "value" defined in the template class from the specialized one. Why? Some one can help me? Thanks

template <class T>
class A {

public:

  int value;

  A() {
    value = 0;
  }

};


template <> class A<int> {

public:

  A() {
    value = 3;  // Use of undeclared identifier 'value'
    A::value = 3; // No member named 'value' in 'A<int>'
    this->value = 3; // No member named 'value' in 'A<int>'
  }

};

Upvotes: 1

Views: 85

Answers (1)

Rakete1111
Rakete1111

Reputation: 48998

An explicit specialization is like a whole new thing. You can't access anything from the explicit specialization of A<int> in the primary template, because it's just like a totally different class.

But, it seems like you want to only specialize the constructor. In that case, you can do that:

template <> 
A<int>::A() {
    value = 3;  // ok
}

This works because you are only specializing the constructor, and the rest of the class is taken from the primary template.

Upvotes: 4

Related Questions