473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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<Streetarr;

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

May 11 '07 #1
3 2409
to*****@gmail.com wrote:
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<Streetarr;

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.
May 11 '07 #2
On May 11, 2:28 am, tome...@gmail.com wrote:
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<Streetarr;

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<Cityand
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
*/
May 11 '07 #3
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[i]);
}

Roman Perepelitsa.

May 11 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
5
by: Gonçalo Rodrigues | last post by:
Hi all, (note: newbie alert) Suppose you have a hierarchy of objects that you deal with via smart pointers, e.g. something like: template<typename T> class Ref { private:
5
by: Milan Cvetkovic | last post by:
Hi, Recently I came accross a weird bug in my code which turned to be my missunderstanding of arrays in C++. It all boils down to the small example: template<class ITER> const char* gggg...
1
by: mathieu | last post by:
Hello there, I am playing around with template metaprograming: I am trying to redefines my own types. But I am facing a small issue, where I cannot describe the whole implementation in one...
7
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am...
11
by: hogtiedtoawaterbuffalo | last post by:
I have a template class that works fine when I implement it with <int>, but when I use <floator <doubleit doesn't work. The class has a dynamic array of type T that gets instantiated in my...
5
by: Szabolcs | last post by:
I am looking for a way to generalize the function template below, so that it will work with any type, not just double. Is this at all possible in C++? I'd like to replace double (*fun)(double)...
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.