473,383 Members | 1,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,383 software developers and data experts.

Work around for the array of references issue

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[i]->foo();
}
return 0;
}

May 29 '06 #1
4 1646
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 29 '06 #2
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
May 29 '06 #3

Tomás wrote:
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:


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

May 29 '06 #4
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
mathieu wrote:
Tomás wrote:
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:


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


May 29 '06 #5

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

Similar topics

6
by: Peter Abel | last post by:
I have an application, which is an instance of a class with a deeply nested object hierarchy. Among others one method will be executed as a thread, which can be stopped. Everything works fine...
2
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
4
by: Barry Young | last post by:
I have Access 2000 installed on a machine and I create the MDE just fine. When I copy the MDE file over to another machine that has Access 2000 installed, the references are hosed and I get unable...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
52
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
31
by: siddhu | last post by:
why can't we have array of references.
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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?

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.