468,765 Members | 1,694 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

For inheriting, do the symbols automatically comes at scope?

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.

Dec 4 '06 #1
3 1245

toton wrote:
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.
>
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.
Dec 4 '06 #2
toton wrote:
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
Dec 4 '06 #3

Salt_Peter wrote:
toton wrote:
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.
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_vectorrange;
typedef boost::sub_range<const range_vectorconst_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<intrvi;
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::ostream_iterator< int>(std::cout,"
"));
std::cout<<std::endl;
boost::sub_range<rvir(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::ostream_iterator<int>( std::cout," "));
std::cout<<std::endl;
boost::sub_range<const rvicr0(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::ostream_iterator<i nt>(std::cout,"
"));
std::cout<<std::endl;
boost::sub_range<const rvicr4(cv.begin()+1,cv.begin()+5);
rvi::const_range cr5(cv.get_range(1,5));

copy(cr4.begin(),cr4.end(),std::ostream_iterator<i nt>(std::cout,"
"));
std::cout<<std::endl;
//copy(cr2.begin(),cr2.end(),std::ostream_iterator<i nt>(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.
Surprisingly, just changing parent class boost::circular_buffer<Tto
std::vector<Tthe code works.
To cover all the possibilities one could write a book on the subject.

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.
Dec 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Torbak | last post: by
1 post views Thread by Torbak | last post: by
5 posts views Thread by eScrewDotCom | last post: by
47 posts views Thread by Pierre Barbier de Reuille | last post: by
24 posts views Thread by toton | last post: by
4 posts views Thread by jumblesale | last post: by
17 posts views Thread by Adrian Hawryluk | last post: by
2 posts views Thread by itaj sherman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.