Template array with pointers

T

tomerdr

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
 
I

Ian Collins

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.
 
S

Salt_Peter

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::eek:stream&
operator<<(std::eek:stream& 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::eek:stream&
operator<<(std::eek:stream& 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
*/
 
R

Roman.Perepelitsa

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.

template <class T>
void StreamIt(ostream & strm, const T & t)
{
strm << t;
}

template <class T>
void StreamIt(ostream & strm, T * p)
{
strm << *p;
}

Now you can use StreamIt to implement operator<< for your
CDynamicArray.

template <class T>
class CDynamicArray
{
....
size_t size() const;
const T & operator[](size_t i) const;
};

template <class T>
ostream & operator<<(ostream & strm, const CDynamicArray<T> & a)
{
for (size_t i = 0; i != a.size(); ++i)
StreamIt(strm, a);
}

Roman Perepelitsa.
 

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,294
Messages
2,571,511
Members
48,216
Latest member
DarrelLho

Latest Threads

Top