C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template array with pointers

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Fri May 11, 2007 9:11 am    Post subject: Template array with pointers Reply with 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
Back to top
Salt_Peter
Guest





PostPosted: Fri May 11, 2007 9:11 am    Post subject: Re: Template array with pointers Reply with quote



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





PostPosted: Fri May 11, 2007 9:11 am    Post subject: Re: Template array with pointers Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.