 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Fri May 11, 2007 9:11 am Post subject: Template array with pointers |
|
|
Hi,
I have been asked to create a dynamic template array as an exercise.
the array should be used as follows:
CDynamicArray<City*> arr; or CDynamicArray<Street> arr;
It also need to support operator << for streaming the content.
(each class also support operator << )
My question is how do i deal with the fact that the array can contains
pointers ?
I need some way to dereference the item (only if needed),in order to
properly streaming it.
Is overloading operator * for all classes is a good idea?
Is there a solution with a template parameters something like:
(DefaultDereference for use with classes)
template<class T,class Dereferencer=DefaultDereferencer<T> >
class CDynamicArray
{
}
Thanks in advance |
|
| Back to top |
|
 |
Salt_Peter Guest
|
Posted: Fri May 11, 2007 9:11 am Post subject: Re: Template array with pointers |
|
|
On May 11, 2:28 am, tome...@gmail.com wrote:
| Quote: | Hi,
I have been asked to create a dynamic template array as an exercise.
the array should be used as follows:
CDynamicArray<City*> arr; or CDynamicArray<Street> arr;
It also need to support operator << for streaming the content.
(each class also support operator << )
My question is how do i deal with the fact that the array can contains
pointers ?
I need some way to dereference the item (only if needed),in order to
properly streaming it.
Is overloading operator * for all classes is a good idea?
Is there a solution with a template parameters something like:
(DefaultDereference for use with classes)
template<class T,class Dereferencer=DefaultDereferencer<T
class CDynamicArray
{
}
Thanks in advance
|
For starters, using an array is the wrong solution. Specially since
you plan for a dynamic container. A std::vector is much. much simpler
to code with.
What problem do you foresee by using CDynamicArray<City*>?
The only difference between CDynamicArray<City> and
CDynamicArray<City*> is the fact that in the former its the container
that owns the 'Cities' and in the latter something else owns the
Cities. The fact that you accessing the cities through a pointer
simply means you have to deference the pointer in op<<. Thats all.
Lets use a toy class S for example and a container named Dynamic which
stores S*:
#include <iostream>
#include <string>
#include <vector>
class S
{
std::string s;
public:
S( const std::string s_ ) : s(s_) { }
friend std::ostream&
operator<<(std::ostream& os, const S& r_s)
{
return os << r_s.s;
}
};
template< typename T >
class Dynamic
{
std::vector< T > m_v;
public:
// default parametixed ctor
Dynamic(size_t sz = 0, const T& t = T())
: m_v(sz, t) { }
// member function
void push_back( const T& t )
{
m_v.push_back( t );
}
// global op<< overload
friend std::ostream&
operator<<(std::ostream& os, const Dynamic< T >& dyn)
{
typedef typename std::vector< T >::const_iterator VIter;
for(VIter viter = dyn.m_v.begin(); viter != dyn.m_v.end(); +
+viter)
{
os << *(*viter); // dereference the ptr at *viter
os << "\n";
}
return os;
}
};
int main()
{
// the vector owns the S's
std::vector< S > vs(10, std::string("strings galore"));
// declare your container
Dynamic< S* > pstrings;
// push_back the addresses of each element
// the above vector holds
for( size_t i = 0; i < vs.size(); ++i)
{
pstrings.push_back( &( vs.at( i ) ) );
}
std::cout << pstrings << std::endl;
}
/*
strings galore
strings galore
strings galore
strings galore
strings galore
strings galore
strings galore
strings galore
strings galore
strings galore
*/ |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Fri May 11, 2007 9:11 am Post subject: Re: Template array with pointers |
|
|
tomerdr (AT) gmail (DOT) com wrote:
| Quote: | Hi,
I have been asked to create a dynamic template array as an exercise.
the array should be used as follows:
CDynamicArray<City*> arr; or CDynamicArray<Street> arr;
It also need to support operator << for streaming the content.
(each class also support operator << )
My question is how do i deal with the fact that the array can contains
pointers ?
I need some way to dereference the item (only if needed),in order to
properly streaming it.
Is overloading operator * for all classes is a good idea?
Rather than operator*(), look at specialising the class for pointer |
types. See Stroustrup 13.5.
--
Ian Collins. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|