473,783 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Self refrence to class type in template

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::s tring, baueue*> queue_table;
typedef std::map<std::s tring> queue;
static queue_table msg_table;
static bqueue* default_queue;
queue msg_queue;
static int push(std::strin g message, bqueue& imsg_queue);
public:
// automatic constructor
~bqueue(); // call unregsister_que ue(*this);
static int register_defaul t_aueue(bqueue& registering_que ue);
static int register_queue( std::string message, bqueue&
registering_que ue);
static int unregister_queu e(bqueue& unregistering_q ueue);
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.registe r_queue("TAG1", object1);
object1.registe r_queue("TAG2", object1);
.....
if(object1.queu ed_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<m y_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.

--
Sincerely,
Greg Phillips


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #1
1 2896
Greg Phillips wrote:
[...]
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::s tring, baueue*> queue_table;
typedef std::map<std::s tring, bqueue*> queue_table;
typedef std::map<std::s tring> queue;
That is impossible. std::map<> requires at least two arguments.

Perhaps you meant

typedef std::queue<std: :string> queue;

?
static queue_table msg_table;
static bqueue* default_queue;
queue msg_queue;
?
static int push(std::strin g message, bqueue& imsg_queue);
public:
// automatic constructor
~bqueue(); // call unregsister_que ue(*this);
static int register_defaul t_aueue(bqueue& registering_que ue);
static int register_queue( std::string message, bqueue&
registering_que ue);
static int unregister_queu e(bqueue& unregistering_q ueue);
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.registe r_queue("TAG1", object1);
object1.registe r_queue("TAG2", object1);
....
if(object1.queu ed_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<m y_queue> my_queue; // error
What error? What is the first 'my_queue'? Is that a type? Why
do you call the object the same? Did you mean to instantiate your
queue somehow differently, like

generic_queue my_queue(my_que ue);

? I am not sure I see the point in that, but it's possible.
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.


If you need to define a list of lists, it's possible, just write

std::list<std:: list<std::list< ...

but you have to stop and make it concrete at some point, like

std::list<std:: list<std::list< std::list<int> > > > blah;

The same with your stuff. There cannot be infinite depth of self-
reference in your type definition, but if you template is generic
enough and complies to the requirements it sets for its argument,
then what to stop you to make it self-contained?

As you probably guessed by now, I am not certain I understand what
you are asking. Self-referencing classes are all over the place,
a linked list is the simplest example. Self-referencing objects
can also exist, a circular queue of one element is something that
might be seen as one. Some stuff is handled in the implementation
and some is inherent to the class itself, like the ability to have
a list of lists comes from the fact that list itself complies with
the requirements list imposes on its contained object...

I believe more information is in order.

Victor
Jul 22 '05 #2

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

Similar topics

9
2405
by: Anthony Heading | last post by:
Hi all, I've often found myself wanting to write code like the example here. Since both MSVC and gcc both reject it, I suspect it is indeed illegal. gcc: no type named `Name' in `class Collection<Animal>' msvc7: error C2039: 'Name' : is not a member of 'Collection<Traits>' But to me it seems pretty unambiguous, so I can't see why it's wrong. Could anybody give me a pointer, either to the standard or the basic
1
3680
by: SpeedBump | last post by:
This is mostly just another "gee it would be nice if it had X" post. Recently I have come across two separate problems which both exhibit the need for a way to self reference when instantiating a template. Example #1: B-trees While implementing a B-Tree I ended up with an (abbreviated) node structure looking something like this:
1
1683
by: Sylvain Audi | last post by:
Hello, I have implemented a data managing template class, that takes care of allocation of objects of type T. It's a simple array of T* that creates new objects and returns its index so that the objects are handled through that index. Now for the fun part. I'm trying to create a template class for the Managed objects, so that each object knows how to find itself (its index and the pointer to the manager).
6
4009
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
10
6109
by: Macka | last post by:
A few pieces of information first: * I have a class called Folder which represents a row of data in a database table. The data access side of things is not an issue. * The table has a parent column which references itself (ie. Adjacency or parent/child model) * I have a public property called 'Parent' which returns me a new reference to a Folder instance containing the data of the parent row.
3
2488
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
3
3758
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
1
2489
by: nw | last post by:
Hi comp.lang.c++, I have the following header (simple.h): #ifndef SIMPLE #define SIMPLE template<class _prec=double> class Simple {
7
5773
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
9643
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
9480
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,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
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
8968
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...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.