Why typename can not be inherented from base class?

P

PengYu.UT

Hi,

I'm wondering why typename can not be inherented from base class. An
example is shown below. If I have to typedef value_type anyway, I think
it would be easier to define the 5 types in iterator like "typedef T
value_type" without inherent from std::iterotor.

Thanks,
Peng

#include <iterator>

template <typename T>
class iterator : public std::iterator<std::random_access_iterator_tag,
T> {
public:
typedef typename std::iterator<std::random_access_iterator_tag,
T>::value_type value_type;//this line can not be deleted
private:
value_type v;
};

int main(){
iterator<int> it;
}
 
V

Victor Bazarov

I'm wondering why typename can not be inherented from base class. An

I am not sure what your question is. What can't be inherited?

template<class T> class Foo {
public:
typedef T* pointer;
};

template<class T> class Bar : public Foo<T> {
public:
typedef typename Foo<T>::pointer pointer; // typedef inherited
private:
pointer ptr;
};
example is shown below. If I have to typedef value_type anyway, I
think it would be easier to define the 5 types in iterator like
"typedef T value_type" without inherent from std::iterotor.

Easier? In what way?
Thanks,
Peng

#include <iterator>

template <typename T>
class iterator : public std::iterator<std::random_access_iterator_tag,
T> {
public:
typedef typename std::iterator<std::random_access_iterator_tag,
T> value_type value_type;//this line can not be deleted
private:
value_type v;
};

int main(){
iterator<int> it;
}

V
 
G

Greg Comeau

I'm wondering why typename can not be inherented from base class. An
example is shown below. If I have to typedef value_type anyway, I think
it would be easier to define the 5 types in iterator like "typedef T
value_type" without inherent from std::iterotor.

#include <iterator>

template <typename T>
class iterator : public std::iterator<std::random_access_iterator_tag,
T> {
public:
typedef typename std::iterator<std::random_access_iterator_tag,
T>::value_type value_type;//this line can not be deleted
private:
value_type v;
};

int main(){
iterator<int> it;
}

The typename is not being singled out, as there is a more general issue:
it has to do with names dependent upon the template parameter. See
http://www.comeaucomputing.com/techtalk/templates/whymembernotfound
 
P

PengYu.UT

Victor said:
I am not sure what your question is. What can't be inherited?

template<class T> class Foo {
public:
typedef T* pointer;
};

template<class T> class Bar : public Foo<T> {
public:
typedef typename Foo<T>::pointer pointer; // typedef inherited
Why can't I simple delete the above line?
private:
pointer ptr;
};


Easier? In what way?
Since "pointer" in the base class is defined from T anyway, why can't I
define "pointer" in iterator(my example) without inherent the base
class std::iterator.
 
V

Victor Bazarov

Why can't I simple delete the above line?

You can't? What gives? Have you tried just deleting it?
^^^^^^^^^^^^
And if you delete it, what happens to this line? You may need to change
it, don't you think?
Since "pointer" in the base class is defined from T anyway, why can't I
define "pointer" in iterator(my example) without inherent the base
class std::iterator.

Can't you?

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,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top