Re: C++ Conversion functions for pointers
From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 05/02/04
- Next message: flavius: "nested functions - _alloc and strings (?)"
- Previous message: Mark : "Re: Question from a Newb"
- In reply to: Paul: "Re: C++ Conversion functions for pointers"
- Next in thread: Paul: "Re: C++ Conversion functions for pointers"
- Reply: Paul: "Re: C++ Conversion functions for pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 02 May 2004 19:40:19 +0200
Paul wrote:
> What exactly are you saying is causing UB here?
> I thought you were meaning the subscript operator function but now I'm
> not so sure what your talking about.
In the code originally posted, you had a statement (paraphraded)
Variant *itsArray = new ObjA[size];
and later you accessed that array through itsArray. That is where the UB
comes into play in the code posted here.
If you start to use an array of pointers, then the UB I referred to is no
longer present.
<snip>
>
> Well just to make it clear..
>
> I know I can create an array of Variant pointers and assign to these
> pointers either Variants or classes derived from variants.
>
> And whether these pointers are in array or not
> i.e:
> Variant* = new Variant(int);
> Variant* = new ClassDerivedfromVariant;
>
> As you can see for built in types I need to create a Variant type ,
> converted to the built in type. So If I were allocating dependant on
> an argument I would first need to check to see if the argument was a
> Variant(or Derived from Variant) before allocating.
>
> This is a bit messy and I would like to do this in one statement.
Unfortunately, C++ simply does not allow that kind of type conversion.
What you could do, is create a design like this:
class VariantArray
{
std::vector<Variant*> array;
VariantArray(const VariantArray&); /* not allowed, for simplicity */
VariantArray& operator=(const VariantArray&); /* not allowed, for
simplicity */
public:
/* Create an array with N pre-allocated, *empty* slots */
VariantArray(int N = 0) : array(N) {}
~VariantArray()
{
for (size_t i=0; i < array.size(); i++)
delete array[i];
}
/* Add something to the array */
template <class T>
void push_back(const T& elem)
{
array.push_back(new T(elem));
}
void push_back(int elem)
{
array.push_back(new Variant(elem));
}
void push_back(float elem)
{
array.push_back(new Variant(elem));
}
/* access operators */
const Variant& operator[](size_t index) const
{
return *array[index];
}
Variant& operator[](size_t index)
{
return *array[index];
}
/* more members */
};
I have deliberately choisen not to make the entire class a template, because
it should hold a mixture of types.
The method used to be able to store both typed derived from Variant and
types that can be stored directly in a Variant object with overloading of
the member(s) that add elements to the array.
<snip>
> I don't want this I wan't to get a Variant*, so I am asking if there
> is a way to change the default behaviour, perhaps by operator overload
> or by using conversion functions or both.
As all pointers are built-in types, you can not alter the way one is
converted to another, or if such a conversion is permitted.
The closest you can probably get is to create a smart-pointer class, but
that is still no silver bullet.
>
> I realise this part of my code had problems and perhaps I should have
> defined more clearly what I was trying to do. The reason being I am
> unsure about how best to construct the data in the Array. I don't yet
> know if an array of Variant* 's is the best solution.
> Perhaps I could store the data in arrays of different types and then
> have some kind of logic inbetween that encapsulates indexing of all
> the arrays into one.
>
> SO I am keeping an open mind about how best to store the different
> types of Variants until I have explored a few different possibilities.
> It's more a design issue than anything.
>
> I have 3 or more options in my mind at the moment and I'm not sure
> which would be best.
>
> 1) Create an array of variant pointers and test for variant classes
> before allocation.
That is very hard to do entirely by hand, but when enlisting the help of the
compiler, it can be turned into a copy/paste excercise.
The compiler can be of assistance if you use overloading (as I did in my
example) or template specialisation.
> 2) Create an array of variant pointers and look into overloading new.
That will not be a viable approach. The operator new() that you can overload
only handles the memory-allocation part. Constructing an actual object in
that memory can not be altered, unless you alter the compiler you are
using.
> 3) create an array of variant pointers for UDT's and and array of
> variants(non pointer) for built in types, then create some kind of
> indexing map to make the two arrays behave like one.
That will be possible, but it will probably give you some bookkeeping
headaches.
<snip>
>
> AHA & TIA.
> Paul.
Bart v Ingen Schenau
-- a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
- Next message: flavius: "nested functions - _alloc and strings (?)"
- Previous message: Mark : "Re: Question from a Newb"
- In reply to: Paul: "Re: C++ Conversion functions for pointers"
- Next in thread: Paul: "Re: C++ Conversion functions for pointers"
- Reply: Paul: "Re: C++ Conversion functions for pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|