For inheriting, do the symbols automatically comes at scope?

T

toton

Hi,
If I inherit some template class to form another template class,
do the member functions from the parent class automatically comes to
the child class, or I need to write something like
using boost::vector<T,Alloc>::begin; etc (to get begin() and other
functions).

Similarly all the base class typedef's automatically come to the
derived class or I need to explicitly say
typedef typename std::vector<T, Alloc>::pointer pointer;

What is according to standard ?
Thanks.
 
S

Salt_Peter

toton said:
Hi,
If I inherit some template class to form another template class,
do the member functions from the parent class automatically comes to
the child class, or I need to write something like
using boost::vector<T,Alloc>::begin; etc (to get begin() and other
functions).

That depends. are the members private, protected or public?
Is the base class a struct or a class?
Was inheritance a public or private inheritance?
And so on... why don't you put together a short example with no
implementation details.
To cover all the possibilities one could write a book on the subject.
 
K

Kai-Uwe Bux

toton said:
Hi,
If I inherit some template class to form another template class,
do the member functions from the parent class automatically comes to
the child class, or I need to write something like
using boost::vector<T,Alloc>::begin; etc (to get begin() and other
functions).

Similarly all the base class typedef's automatically come to the
derived class or I need to explicitly say
typedef typename std::vector<T, Alloc>::pointer pointer;

What is according to standard ?

They are inherited, but they become dependent names. E.g.:

template < typename T >
struct bad_practice : std::vector<T> {};

int main ( void ) {
bad_practice<int>::size_type i;
}

should work. However

template < typename T >
struct bad_practice : std::vector<T> {

size_type i;

};

should not.


Best

Kai-Uwe Bux
 
T

toton

Salt_Peter said:
That depends. are the members private, protected or public?
Is the base class a struct or a class?
Was inheritance a public or private inheritance?
And so on... why don't you put together a short example with no
implementation details.

Here is an example I was trying to compile, and spending hard hours :(
..
The example uses a few boost library though, and have a second problem
Here I was using boost::range mostly. But I failed to compile it using
VS7.1 and VS2005. They were both saying some type conversion problem
(Though I don't find any, it it is hard to debug templates), while
mingw was complaining about using thing, and after that it compiles
finely.
Here is the complete source code (boost rc 1_34 is used , along with
one sandbox circular_buffer class )
#include <iterator>
#include <memory>
#include <vector>
#include <iostream>
using namespace std;
#include <boost/circular_buffer.hpp>
#include <boost/range.hpp>

template<typename T>
class range_vector : public boost::circular_buffer<T>{
public:
typedef boost::sub_range<range_vector> range;
typedef boost::sub_range<const range_vector> const_range;
typedef typename boost::circular_buffer<T>::size_type size_type;
///mingw complains, doesn't compile without this.
using boost::circular_buffer<T>::begin; ///mingw complains without
this. doesn't compile
range_vector(size_type capacity) :
boost::circular_buffer<T>(capacity){}
range get_range(size_type from,size_type to){
std::cout<<"range"<<std::endl;
return range(begin()+from,begin()+to);
}
const_range get_range(size_type from,size_type to)const{
std::cout<<"const range"<<std::endl;
return const_range(begin()+from,begin()+to);
}
};
typedef range_vector<int> rvi;
int main() {
rvi v(9);
int x[] = {0,1,2,3,4,5,6,7,8};
v.insert(v.end(),x,x+9);
const rvi& cv = v;
std::copy(v.begin(),v.end(),std::eek:stream_iterator<int>(std::cout,"
"));
std::cout<<std::endl;
boost::sub_range<rvi> r(v.begin()+1,v.begin()+5);
rvi::range r0(v.begin()+1,v.begin()+5);
rvi::range r1(v.get_range(1,5));
r[0] = 100;
copy(r.begin(),r.end(),std::eek:stream_iterator<int>(std::cout," "));
std::cout<<std::endl;
boost::sub_range<const rvi> cr0(v.begin()+1,v.begin()+5);
rvi::const_range cr1(v.begin()+1,v.begin()+5);
rvi::const_range cr2(v.get_range(1,5));

std::cout<<cr0[0]<<std::endl;
rvi::const_range cr3 = v.get_range(1,5);
copy(cr3.begin(),cr3.end(),std::eek:stream_iterator<int>(std::cout,"
"));
std::cout<<std::endl;
boost::sub_range<const rvi> cr4(cv.begin()+1,cv.begin()+5);
rvi::const_range cr5(cv.get_range(1,5));

copy(cr4.begin(),cr4.end(),std::eek:stream_iterator<int>(std::cout,"
"));
std::cout<<std::endl;
//copy(cr2.begin(),cr2.end(),std::eek:stream_iterator<int>(std::cout,"
"));

std::cout<<std::endl;
std::cout<<std::endl;
return 0;
}

But with that addition everything works for MinGW. (gcc version 3.4.5
mingw build)
VS 2003 or 2005 don't give any error or warning without them . but it
fails to compile
saying
e:\boost_1_33_1\boost\range\iterator_range.hpp(60): error C2440: 'type
cast' : cannot convert from 'boost::range_iterator<C>::type' to
'boost::range_iterator<C>::type'
with
[
C=const boost::sub_range<range_vector<int>>
]
and
[
C=range_vector<int>
]

Which I am not sure why it can't.
 

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

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top