Hi, I'm trying to create a class template like the following:
template <class Type>
class MyVector
{
MyVector();
~MyVector();
unsigned int MyCount;
Type* MyData;
}
where the destructor is defined like:
template <class Type>
MyVector<Type>: :~MyVector()
{
delete [] MyData;
}
The problem is that when Type is something like a pointer to an object
then the destructor needs to delete each of those pointers before
deleting MyData, right? But if Type is an integral type such as
"unsigned int" then I don't need (or want) to do this....
So I thought that I might be able to use a specialization, but it
seems that there are too many cases to cover, no? Am I missing
something? Thanks for any direction! 2 1706
"Erik Friis" <ef****@sprintm ail.com> wrote in message
news:ce******** *************** ***@posting.goo gle.com... Hi, I'm trying to create a class template like the following:
template <class Type> class MyVector { MyVector(); ~MyVector();
unsigned int MyCount; Type* MyData; }
where the destructor is defined like:
template <class Type> MyVector<Type>: :~MyVector() { delete [] MyData; }
The problem is that when Type is something like a pointer to an object then the destructor needs to delete each of those pointers before deleting MyData, right?
Well that is up to you. But I would say, wrong.
You are saying that MyVector should take ownership of any pointers added to
it. I would say that is poor design, just adding a pointer to a MyVector
doesn't mean that it is responsible for it's destruction. Consider
T* ptr = something();
if (ptr != 0)
{
MyVector<T*> vec_of_pointers ;
vec_of_pointers .add(ptr);
...
} // ptr will be deleted here
ptr->something_else (); // *** CRASH ***, ptr has already been deleted
Do you consider that behaviour surprising? I do, but as I say it's up to
you.
What about this?
{
MyVector<char*> vec_of_pointers ;
vec_of_pointers .add("watch me crash!!!");
} // crash here, attempting to delete a string literal
That behaviour would make me cross.
But if Type is an integral type such as "unsigned int" then I don't need (or want) to do this....
So I thought that I might be able to use a specialization, but it seems that there are too many cases to cover, no? Am I missing something? Thanks for any direction!
You can use partial specialisation.
template <class Type>
class MyVector<Type*>
{
MyVector();
~MyVector();
unsigned int MyCount;
Type* MyData;
};
This is a specialised version of MyVector solely for pointers. Put this
class after your main MyVector class if you still think its a good idea.
john
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2s******** *****@uni-berlin.de... "Erik Friis" <ef****@sprintm ail.com> wrote in message news:ce******** *************** ***@posting.goo gle.com... Hi, I'm trying to create a class template like the following:
template <class Type> class MyVector { MyVector(); ~MyVector();
unsigned int MyCount; Type* MyData; }
where the destructor is defined like:
template <class Type> MyVector<Type>: :~MyVector() { delete [] MyData; }
The problem is that when Type is something like a pointer to an object then the destructor needs to delete each of those pointers before deleting MyData, right? Well that is up to you. But I would say, wrong.
You are saying that MyVector should take ownership of any pointers added
to it. I would say that is poor design, just adding a pointer to a MyVector doesn't mean that it is responsible for it's destruction.
The whole issue of pointers and ownership is important enough that it needs
it's own solution, quite separate from anything that you might do in
MyVector. Look up 'smart pointers' in your favourite C++ book.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Patrick Kowalzick |
last post by:
Dear all,
I have a question about default template parameters.
I want to have a second template parameter which as a default parameter,
but depends on the first one (see below). Is something like that possible?
Some workaround?
Thank you,
Patrick
|
by: Dave |
last post by:
Hello all,
Consider this function template definition:
template<typename T>
void foo(T) {}
If foo is never called, this template will never be instantiated.
Now consider this explicit instantiation of foo:
|
by: nikola |
last post by:
Hi all,
I was working with a simple function template to find the min of two values.
But since I would like the two values to be different (type) I dont know
what kind of value (type) it will return. I tried to write something like
this:
template <class Type1, class Type2, class Type3>
Type3 findMin(Type1 x, Type2 y){
return (x < y) ? x : y;
|
by: Siemel Naran |
last post by:
Hi. I have a function
template <class InputIter, class OutputIter>
void f(InputIter begin, InputIter end, OutputIter result);
With c of type char* and cc of type const char*, the code f(c,c,cc) calls
f<char*, const char *>, which is fine.
But f(c,c,c) calls a new instantiation f<char*,char*> whereas I'd like it to
call f<const char*,char*>.
|
by: Amit |
last post by:
Greetings all,
I am writing some code somehwat similar to the test code I have below. I am
having a variety of issues with template specialization. I am not sure if
this is related to something i havent correctly understood
related to template specialization or is it some problem related to the
compiler.
Following is the code..
| |
by: Alfonso Morra |
last post by:
if I have a class template declared as ff:
(BTW is this a partial specialization? - I think it is)
template <typename T1, myenum_1 e1=OK, my_enum_2=NONE>
class A {
public:
A();
virtual ~A() ;
|
by: Marek Vondrak |
last post by:
Hello.
I have written the following program and am curious why it prints "1" "2".
What are the exact effects of explicitly providing function template
parameters at the call? Is the second assign() function really a
specialization of the first assign() or is it an assign() overload?
Thank you.
-- Marek
|
by: merdem |
last post by:
Hi all,
I just started to mess around with templates. First I declared a class
Image as follows(this is a small version of the real thing which is
pretty big):
template<int depth, int space, int channel>
class Image {
// Ctor/Dtor.
|
by: Barry |
last post by:
The following code compiles with VC8
but fails to compiles with Comeau online,
I locate the standard here:
An explicit specialization of any of the following:
|
by: abir |
last post by:
i have a template as shown
template<typename Sclass Indexer{};
i want to have a specialization for std::vector both const & non const
version.
template<typename T,typename Aclass Indexer<std::vector<T,A {}
matches only with nonconst version. anyway to match it for both? and
get if it is const or nonconst?
Actually i want 2 specialization, one for std::vector<T,Aconst & non
const other for std::deque<T,Aconst & non const.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |