Inheritance & scope - correct addressing

T

Thomas.Zauner

Hi again,

I have a quite old code to reactivate (still), and ran into the
following problem:
(This is a boild down ecample that doesn't do anything, i just want to
state the problem)

----------------------------------------------------------------------------------------------------------
template <class T , int N>
class Rec: public Rec<T,N-1>
{
public:
Rec(T i):Rec<T,N-1>(i){};
inline void access(T newt)
{
base_var=newt; // ERROR "not in scope"
//this->base_var=newt;
//Rec<T,1>::base_var=newt;
//Rec<T,N-1>::base_var=newt;
}

};

template <class T>
class Rec<T,1>
{
public:
Rec(T i){ base_var=i;};
protected:
T base_var;
};


int main(void)
{
Rec<int,3> int_vec(0);
int_vec.access(5);
return 0;
};
----------------------------------------------------------------------------------------------------------
Now my question.
Which of the three options would be "correct"

//this->base_var=newt;
//Rec<T,1>::base_var=newt;
//Rec<T,N-1>::base_var=newt;

and what is the difference? is there any?

Thank you in advance
Thomas
 
V

Victor Bazarov

I have a quite old code to reactivate (still), and ran into the
following problem:
(This is a boild down ecample that doesn't do anything, i just want to
state the problem)

----------------------------------------------------------------------------------------------------------
template <class T , int N>
class Rec: public Rec<T,N-1>
{
public:
Rec(T i):Rec<T,N-1>(i){};
inline void access(T newt)
{
base_var=newt; // ERROR "not in scope"
//this->base_var=newt;
//Rec<T,1>::base_var=newt;
//Rec<T,N-1>::base_var=newt;
}

};

template <class T>
class Rec<T,1>
{
public:
Rec(T i){ base_var=i;};
protected:
T base_var;
};


int main(void)
{
Rec<int,3> int_vec(0);
int_vec.access(5);
return 0;
};
----------------------------------------------------------------------------------------------------------
Now my question.
Which of the three options would be "correct"

//this->base_var=newt;
//Rec<T,1>::base_var=newt;
//Rec<T,N-1>::base_var=newt;

and what is the difference? is there any?

The last one or the first one. The second one is apparently not
a correct choice, at least according to your apparent algorithm.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top