Afficher un message
Vieux 30/06/2008, 09h24   #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
 
Page generated in 0,07636 seconds with 9 queries