473,812 Members | 3,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create static array of template class object?

I have a template class object that I use as a mesaging queue between
threads. I use it as a static object that I initialize like so:

foo.h
class foo{
static LFQueue<const char*,100lfqMyQ ueue;
};
foo.cpp
LFQueue<const char*,100Foo::l fqMyQueue;

This has worked fine for me. The problems that I've run into is when
I've attempted to get tricky and try and declare a number of LFQueue
objects in a static array, so that I can access a number of queues out
of a single object. I tried to do it like so:

foo.h
class foo{
static LFQueue<const char*,100aLFQAr ray[3];
};
foo.cpp
LFQueue<const char*,100Foo::a LFQArray[3];

This compiles ok, but when I use it, the const char data goes out of
scope and I get garbage when trying to read from the queue. I'm not
sure if I failed to set it up right, or if the way the queue template
class is setup makes this impossible. It's almost like the array is
static, but the LFQueue elements are not?

Anyway, if any of you could shed some light on how I could go about
this given what you see above, that would be great. If you need more
info I can post the LFQueue Template class code, but I wanted to make
sure it wasn't something obvious with the way this was being declared.

Many thanks!
Haley
Jun 30 '08 #1
1 3389
On Jun 30, 4:28 am, flowstudi...@gm ail.com wrote:
I have a template class object that I use as a mesaging queue
between threads. I use it as a static object that I initialize
like so:
foo.h
class foo{
static LFQueue<const char*,100lfqMyQ ueue;};
foo.cpp
LFQueue<const char*,100Foo::l fqMyQueue;
This has worked fine for me. The problems that I've run into
is when I've attempted to get tricky and try and declare a
number of LFQueue objects in a static array, so that I can
access a number of queues out of a single object. I tried to
do it like so:
foo.h
class foo{
static LFQueue<const char*,100aLFQAr ray[3];};
foo.cpp
LFQueue<const char*,100Foo::a LFQArray[3];
This compiles ok, but when I use it, the const char data goes
out of scope and I get garbage when trying to read from the
queue.
You're not being very specific, but if the first example really
works (and doesn't just seem to), then this should work as well.
It sounds, however, like you have problems with the lifetime
management of the objects pointed to by the char const*; as long
as you only pass string literals, there should be no problem,
but you can't pass much of anything else without some specific
lifetime management convensions.

Supposing that 1) you've provided correct synchronization in the
LFQueue class and 2) the implementation of std::string in your
compiler is thread safe (usually the case today), then you can
pass std::string without problems. Otherwise, I've had very
good results using a queue with std::auto_ptr at the interface
level (for polymorphic message objects, which can't be copied);
once the "object" is in the queue, the sending thread no longer
has access to it.
I'm not sure if I failed to set it up right, or if the way the
queue template class is setup makes this impossible. It's
almost like the array is static, but the LFQueue elements are
not?
Which objects? The char const* themselves should be managed by
the queue---if the queue is based on an std::deque (the obvious
and simplest solution), then they won't be static, but rather in
dynamically allocated memory (but std::deque will copy and
maintain them as needed). Obviously, the lifetime of whatever
the char const* points to is your business, which is why I said
that in practice, you can probably only use string literals.
(Or you do a new char[], and the receiver does a delete[]. But
that's just stupid, when you have a perfectly good class with
value semantics, std::string, available.)
Anyway, if any of you could shed some light on how I could go
about this given what you see above, that would be great.
Given that from what we see above, we have no idea how LFQueue
works, it's hard to say much. (If, for example, not only does
LFQueue have correct synchronization , but it also is specialized
to do a deep copy of the char const*, then I can't see a reason
why it wouldn't work. I'm willing to bet that this isn't the
case, however.)
If you need more info I can post the LFQueue Template class
code, but I wanted to make sure it wasn't something obvious
with the way this was being declared.
We need both the LFQueue code, and at least a minimal example of
how it is being used (what you are passing, etc.).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 30 '08 #2

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

Similar topics

1
7023
by: Dave | last post by:
In Exceptional C++, the following code snippet appears at the bottom of page 197: template<class T> class Array : private ArrayBase, public Container { typedef Array AIType; public: Array( size_t startingSize = 10 ) : Container( startingSize ),
3
1429
by: dingounan | last post by:
a static array:smart_hash it's a usual way to use native array in C++: //this is a special "sort" algorithm #include <iostream> using namespace std; int main() {
4
2002
by: santosh | last post by:
Hello, I have a doubt about static variable. class B { private: static int x; public: static set(int y) {
7
8876
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
5
1926
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2; // ... public static MyObject Object400;
23
7429
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
2
2417
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
3
4490
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee. Here is my Inventory program from 1 to 3: package...
7
2164
by: Rob | last post by:
This actually compiles and works but it doesn't seem like the best code, so I was wondering is there another way to do this? template <typename Tvector<T>* addDepth(T) { return new vector<T>; } ....a templated meth...
0
9734
marktang
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...
0
9607
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10417
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9220
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6897
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();...
0
5568
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...
1
4357
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
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
bsmnconsultancy
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...

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.