PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > How to create static array of template class object?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to create static array of template class object?

Réponse
 
LinkBack Outils de la discussion
Vieux 30/06/2008, 03h28   #1
flowstudioLA@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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*,100> lfqMyQueue;
};
foo.cpp
LFQueue<const char*,100> Foo::lfqMyQueue;

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*,100> aLFQArray[3];
};
foo.cpp
LFQueue<const char*,100> Foo::aLFQArray[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
  Réponse avec citation
Vieux 30/06/2008, 08h24   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to create static array of template class object?

* flowstudioLA@gmail.com:
> 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*,100> lfqMyQueue;
> };
> foo.cpp
> LFQueue<const char*,100> Foo::lfqMyQueue;


You're using threads and you're using char* pointers for communication between
them, and packaging things in templates just for added complexity.

That means that it should not be surprising if things don't work.


> 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*,100> aLFQArray[3];
> };
> foo.cpp
> LFQueue<const char*,100> Foo::aLFQArray[3];


The declarations should work fine.

But possibly you haven't shown all relevant code.



> This compiles ok, but when I use it, the const char data goes out of
> scope


That's meaningless.


> and I get garbage when trying to read from the queue.


Not surprising.


> 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?


It's almost like as if doing threading and mixing in char* and perhaps other raw
pointers, and mixing in some templating, at beginner's level, is a recipe for
disaster?

Yes?

You're concentrating on the wrong things, both wrt. what could cause the
failure, and wrt. learning C++ programming.


> 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.


The declaration seems to be OK.

Try to reduce the problem to a minimum, non-threading program that you can post
in its entirety here (compilable by others).


Cheers, & hth.,

- Alf

--
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?
  Réponse avec citation
Vieux 30/06/2008, 09h36   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to create static array of template class object?

On Jun 30, 4:28 am, flowstudi...@gmail.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*,100> lfqMyQueue;};


> foo.cpp
> LFQueue<const char*,100> Foo::lfqMyQueue;


> 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*,100> aLFQArray[3];};


> foo.cpp
> LFQueue<const char*,100> Foo::aLFQArray[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:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h19.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13010 seconds with 11 queries