Work around for the array of references issue

M

mathieu

Hello,

I would like implement a nice way to work around the array of
references issue in C++. What do usually people do ? Do you maintain a
separate factory/pool of elements as part of the API ?
So in my example (*) I would need to either :

1. allocate on the heap (and then fragment memory):
void fill(A *array[])
{
A *a = new A;
B *b = new B;
array[0] = a;
array[1] = b;
}

or have a mechanism that store elements of the same type in individual
arrays:

A PoolA[100];
B PoolB[100];
void fill(A *array[])
{
A *a = GetNextFromPoolA()
B *b = GetNextFromPoolB();
array[0] = a;
array[1] = b;
}
GetNextFromPoolA/B look like very complex tasks to implement. Does this
correspond to any known design pattern ?

Comments suggestion very welcome !
Mathieu

(*) Small dumb demonstration of problem:
#include <iostream>

struct A {
virtual void foo() {
std::cout << "A" << std::endl;
}
};
struct B : public A {
virtual void foo() {
std::cout << "B" << std::endl;
}
};
void fill(A *array[])
{
A a;
B b;
array[0] = &a;
array[1] = &b;
}
int main()
{
const int size = 2;
A* array[size]; // Have to use pointers...
fill(array);
for(int i=0; i<size;++i) {
array->foo();
}
return 0;
}
 
A

Alf P. Steinbach

* mathieu:
I would like implement a nice way to work around the array of
references issue in C++.

Presumably you mean, that you need to use pointers to have run-time
polymorphic array elements.

What do usually people do ?

Sleep and wake, eat and excrete, work and relax, have sex or not have
sex, fall in and out of love, and so on; programming is just part of it.

Do you maintain a
separate factory/pool of elements as part of the API ?
So in my example (*) I would need to either :

1. allocate on the heap (and then fragment memory):
void fill(A *array[])
{
A *a = new A;
B *b = new B;
array[0] = a;
array[1] = b;
}

or have a mechanism that store elements of the same type in individual
arrays:

A PoolA[100];
B PoolB[100];
void fill(A *array[])
{
A *a = GetNextFromPoolA()
B *b = GetNextFromPoolB();
array[0] = a;
array[1] = b;
}
GetNextFromPoolA/B look like very complex tasks to implement. Does this
correspond to any known design pattern ?

Generally a cache. One simple way to do a cache is a free-list. But
don't bother about that, or about operational efficiency at all: try to
make the code correct first.

That means std::vector (with known size), using boost::shared_ptr
(directly solving most of the unsafety problem), and the like.
 
T

Tomás

I would like implement a nice way to work around the array of
references issue in C++. What do usually people do ?


Spend enough time at the keyboard and you'll probably figure it out.
Here's what I got in the first fifteen minutes:

template<class T>
class Reference {
private:

T &ref;

public:

Reference( T &referent ) : ref(referent) {}

operator const T&() const { return ref; }

operator T&() { return ref; }
};

int main()
{
int a = 5;
char *p = 0;
unsigned const b = 3;
double const k = 45.32;

Reference<int> ra(a);
Reference<char*> rp(p);
Reference<unsigned const> rb(b);
Reference<double const> rk(k);

int z = 1, y = 2, x = 3, w = 4, v = 5;

Reference<int> array[5] = { z, y, x, w, v };


int i = 5;

i = array[2];

array[3] = 45;
/* Some tweaking necessary for the above line */
}


-Tomás
 
M

mathieu

Tomás said:
Spend enough time at the keyboard and you'll probably figure it out.
Here's what I got in the first fifteen minutes:

Hi Tomás,

I am sorry I poorly formulated my question. Alf manage though to
reformulate properly (see his post) into :
....
Presumably you mean, that you need to use pointers to have run-time
polymorphic array elements.
....

That's indeed my problem, for which I am looking right now into the
boost::shared_ptr as suggested.
Mathieu
 
B

boaz_sade

Hi
If you're looking into boost (which is a smart move), and you need
arrys of pointers then better use Pointer Container Library, which was
designed to hold pointers to objects (where the "normal STL container"
are not the best choice)
good luck
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top