473,406 Members | 2,843 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,406 software developers and data experts.

How to be sure my array only takes pointer

Hi,

I would like to create a class called ArrayPtr that could only store
pointers. How can I be sure that arguments passed is a pointer ?
template<typename Tclass ArrayPtr : public std::vector<T>
{
public:

inline void Add(T object)
{
push_back(object);
}

inline void Clear()
{
clear();
}

int GetCount()
{
return size();
}
};
The goal is to empty my vector and to deallocate pointer when I call
Clear().
Aug 25 '06 #1
7 2175
Vincent RICHOMME posted:
Hi,

I would like to create a class called ArrayPtr that could only store
pointers. How can I be sure that arguments passed is a pointer ?

Perhaps some compile-time assertions along the following lines:

(This won't compile with g++, not sure why.)

#include <vector>

#define ENSURE_LEGALITY_OF_EXPRESSION(expr) \
int (*VerifyFuncDecl())[(unsigned)!!sizeof(expr)]

template<int i>
struct VerifyType {
unsigned bitfield_cant_be_negative : i;
};

#define COMPILE_TIME_ASSERT(expr) \
int (*VerifyFuncDecl())[(unsigned)!!sizeof(VerifyType<(expr)?1:-1>)]

template<class T>
class PtrArray : public std::vector<T>
{
private:

ENSURE_LEGALITY_OF_EXPRESSION( *T() );

COMPILE_TIME_ASSERT( sizeof(T) <= sizeof(void*) );

using std::vector<T>::push_back;
using std::vector<T>::clear;
using std::vector<T>::size;

public:

inline void Add(T const ptr)
{
push_back(ptr);
}

inline void Clear()
{
clear();
}

int GetCount()
{
return size();
}
};

int main()
{
PtrArray<char*arr;
}

--

Frederick Gotham
Aug 25 '06 #2
Vincent RICHOMME wrote:
Hi,

I would like to create a class called ArrayPtr that could only store
pointers. How can I be sure that arguments passed is a pointer ?

If you don't want to expose all the methods of vector, how about

template<typename Tclass ArrayPtr : std::vector<T*>
{
public:

void Add(T* object)
{
push_back(object);
}

template<typename Tclass ArrayPtr : public std::vector<T>
{
public:

inline void Add(T object)
Get rid of the 'inline' here.

--
Ian Collins.
Aug 25 '06 #3
Vincent RICHOMME wrote:
Hi,

I would like to create a class called ArrayPtr that could only store
pointers. How can I be sure that arguments passed is a pointer ?
template<typename Tclass ArrayPtr : public std::vector<T>
{
public:

inline void Add(T object)
{
push_back(object);
}

inline void Clear()
{
clear();
}

int GetCount()
{
return size();
}
};
The goal is to empty my vector and to deallocate pointer when I call
Clear().

What about:

template < typename T >
class ArrayPtr;
// no generic implementation !

template < typename T >
class ArrayPtr<T*: public std::vector<T*>
// partial specialization for pointer types provided
{
// whatever
}
Do not provide a specialization for non-pointer types. You can add
specializations for smart-pointers as needed.
Best

Kai-Uwe Bux

ps.: public inheritance from std::vector is rarely a good idea. Your
particular need is a little unclear but it does not look like an exception
to the rule of thumb.
Aug 25 '06 #4
Vincent RICHOMME wrote:
I would like to create a class called ArrayPtr that could only store
pointers. How can I be sure that arguments passed is a pointer ?
See Fredericks posting, which works in principle (VerifyFuncDecl was
declared twice, so you can only use his macro once in a class definition).

If you want to delete pointers, you know they're C-style pointers, so
you can just take the value type as a template argument:

template<typename Tclass ArrayPtr : public std::vector<T*>
{
public:
....

However, this is probably not something you want to do.

You generally not use delete but use libraries that do the management
for you. If you have access to boost or tr1, use a vector of shared_ptrs.

If not, build your own smart pointer or build the memory management into
a base class of your managed objects. Don't mess around with a memory
managed array unless you have some special reason to do it.

Jens
Aug 25 '06 #5
Kai-Uwe Bux wrote:
>
What about:

template < typename T >
class ArrayPtr;
// no generic implementation !
or even

template < typename T >
class ArrayPtr
{
private:
~ArrayPtr();
};

to have a build error instead of a link one.

In general, the best one is to let the object deallocate itself when no
more referenced (using a smart pointer), instead of adding this logic to
the container.

Bye,

Zeppe
Aug 25 '06 #6

Vincent RICHOMME skrev:
Hi,

I would like to create a class called ArrayPtr that could only store
pointers. How can I be sure that arguments passed is a pointer ?
When I originally saw your post, I thought in the same direction as
Kai-Uwe. But it is obvious to me now that Ian Collins solution is much
better. It is also coherent with e.g. std::auto_ptr (it is
std:auto_ptr<int - not auto_ptr<int*>).
One exception is that I must agree that inheriting from std::vector is
a bad idea. And Jens Theisen is also correct that boost is a good
place to check out. I believe that they have not only shared_ptr but
also some pointer containers. Check that site out!

/Peter
The goal is to empty my vector and to deallocate pointer when I call
Clear().
Aug 25 '06 #7
"Vincent RICHOMME" <ri******@free.frwrote in message
news:44***********************@news.free.fr...
Hi,

I would like to create a class called ArrayPtr that could only store
pointers. How can I be sure that arguments passed is a pointer ?
template<typename Tclass ArrayPtr : public std::vector<T>
{
public:

inline void Add(T object)
{
push_back(object);
}

inline void Clear()
{
clear();
}

int GetCount()
{
return size();
}
};
The goal is to empty my vector and to deallocate pointer when I call
Clear().
How about this?

template< typename T >
void ArrayPtr<T>::Clear()
{
int count = this->size();
while ( count )
// if you get a compilation error in here
// (C2541: 'delete' : cannot delete objects that are not pointers)
// it's because the type you're using to store in this array is not
a pointer type
// or the class does not have accessible destructor.
// Fix the problem in the class or use a different class.
delete this->operator[]( --count );

this->std::vector<T>::clear();
}

JMu
Aug 26 '06 #8

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
5
by: ali | last post by:
Hi, I'm trying to understand the reason for different output on the following codes Code1: #include <iostream.h> int main()
6
by: m sergei | last post by:
main(int argc, char *argv) the second parameter to function main takes a pointer to an array. can we also write it in terms of a reference rather than a pointer ? an example of using (usage)...
9
by: Christopher Benson-Manica | last post by:
Recently, the following definition was posted to comp.lang.c++: > char **Arr; 1) Is the type of this object "array of 3 (array of two of pointer to pointer to char)"? If not, what is it? ...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
20
by: subramanian | last post by:
Hello I have a doubt in the following piece of code: int a; printf("a=%p\n", a); printf("&a=%p\n", &a); these printf statements print the same value for both 'a' and '&a". I tried in...
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.