G
Greg Phillips
Hello everyone,
I have read the section on templates in The C++ Programming Language 3rd
edition 3
times over the last 4 days. Still I am stumped. I'll explain a bit about
what I am doing
before asking the question so as you can understand what I am trying to
accomplish.
Any help would be much appreciated.
Lets say we are implementing a message sorting system where the messages are
strings in
the form of:
TAG message...
This can be encapsulated so message could be a form of TAG message also. So
you could
have a message like TAG TAG TAG message, where each tag would be aimed at a
system
further down the line (somewhat like UUCP).
My first run at this went something like:
class bqueue {
private:
typedef std::map<std::string, baueue*> queue_table;
typedef std::map<std::string> queue;
static queue_table msg_table;
static bqueue* default_queue;
queue msg_queue;
static int push(std::string message, bqueue& imsg_queue);
public:
// automatic constructor
~bqueue(); // call unregsister_queue(*this);
static int register_default_aueue(bqueue& registering_queue);
static int register_queue(std::string message, bqueue&
registering_queue);
static int unregister_queue(bqueue& unregistering_queue);
static int sort_queue(std::string message);
std::string pop();
bool queued_messages();
};
This being the class where messages comming into the system would go. If an
object needed
messages that had a TAG no deeper than the first level they could:
bqueue object1;
object1.register_queue("TAG1", object1);
object1.register_queue("TAG2", object1);
.....
if(object1.queued_messages())
message = object1.pop();
The second, third, forth, ...., level messages could be handled by a class
almost exactly the same
except it would have a bool to mark one instantiation master and a way to
register messages with
the queue right before it in the stream.
That is the basics of what I am doing. Here is where the problem is and
where I am stumped.
Because the class refrences itself as a type to be able to access
instantiations of itself to be able to
push messages on the proper queues, and because it relys on the nature of
static variables and static
functions to do it's work, diriving the second class from the first wouldn't
make much sense since the
static functions that refrence itself would need rewrote. You would end up
with two copies of almost
identical code to maintain. Also it would be nice to be able to declare a
generic_queue as to be able to
create down stream routing systems if need be.
This class works great as a concreate class but there is one major problem
in defining it as a template:
template<class T> class generic_queue { .... }; // where T replaces all the
self refrences
generic_queue<my_queue> my_queue; // error
I need to to know figure out how to define a template class that refrences
itself, and that the refrences to
itself is the generic type. That way it would be possible to declare
diffrent class types with the same
functionality, hence seprate message queueing systems.
Like I said, any help would be much appreciated.
I have read the section on templates in The C++ Programming Language 3rd
edition 3
times over the last 4 days. Still I am stumped. I'll explain a bit about
what I am doing
before asking the question so as you can understand what I am trying to
accomplish.
Any help would be much appreciated.
Lets say we are implementing a message sorting system where the messages are
strings in
the form of:
TAG message...
This can be encapsulated so message could be a form of TAG message also. So
you could
have a message like TAG TAG TAG message, where each tag would be aimed at a
system
further down the line (somewhat like UUCP).
My first run at this went something like:
class bqueue {
private:
typedef std::map<std::string, baueue*> queue_table;
typedef std::map<std::string> queue;
static queue_table msg_table;
static bqueue* default_queue;
queue msg_queue;
static int push(std::string message, bqueue& imsg_queue);
public:
// automatic constructor
~bqueue(); // call unregsister_queue(*this);
static int register_default_aueue(bqueue& registering_queue);
static int register_queue(std::string message, bqueue&
registering_queue);
static int unregister_queue(bqueue& unregistering_queue);
static int sort_queue(std::string message);
std::string pop();
bool queued_messages();
};
This being the class where messages comming into the system would go. If an
object needed
messages that had a TAG no deeper than the first level they could:
bqueue object1;
object1.register_queue("TAG1", object1);
object1.register_queue("TAG2", object1);
.....
if(object1.queued_messages())
message = object1.pop();
The second, third, forth, ...., level messages could be handled by a class
almost exactly the same
except it would have a bool to mark one instantiation master and a way to
register messages with
the queue right before it in the stream.
That is the basics of what I am doing. Here is where the problem is and
where I am stumped.
Because the class refrences itself as a type to be able to access
instantiations of itself to be able to
push messages on the proper queues, and because it relys on the nature of
static variables and static
functions to do it's work, diriving the second class from the first wouldn't
make much sense since the
static functions that refrence itself would need rewrote. You would end up
with two copies of almost
identical code to maintain. Also it would be nice to be able to declare a
generic_queue as to be able to
create down stream routing systems if need be.
This class works great as a concreate class but there is one major problem
in defining it as a template:
template<class T> class generic_queue { .... }; // where T replaces all the
self refrences
generic_queue<my_queue> my_queue; // error
I need to to know figure out how to define a template class that refrences
itself, and that the refrences to
itself is the generic type. That way it would be possible to declare
diffrent class types with the same
functionality, hence seprate message queueing systems.
Like I said, any help would be much appreciated.