473,657 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is a list template class needed?

Both class A and B inherites from class C. A std::list is used to contain a
list of objects of class A or a list of objects of class B, but not both
types of classes at the same time.

How to use template to realize this? Thanks!
Jul 22 '05 #1
4 1247
"cass" <ca******@knowh ouse.com> wrote in message news:mIHgc.5117 0
Both class A and B inherites from class C. A std::list is used to contain a list of objects of class A or a list of objects of class B, but not both
types of classes at the same time.

How to use template to realize this? Thanks!


If you use a list<C> you can hold both A and B objects in the same list.
But you don't have to take advantage of this -- ie. you can set it up so
that all the objects will be A or all will be B.

Actually, in C++ you have to use something like
std::list<boost ::shared_ptr<C> > and ensure that C::~C() is virtual.

In your list insert function you can enforce all items have the same type.

class mylist : private std::list<boost ::shared_ptr<C> > {
typedef boost::shared_p tr<C> smart_pointer_t ype;
typedef std::list<smart _pointer_type> list_base;
public:
class inconsistent_ty pe { };
typedef list_base::size _type size_type;
size_type size() const { return list_base::size (); }
C& back() { return *list_base::bac k(); }
const C& back() const { return *list_base::bac k(); }
void push_back(std:: auto_ptr<C> newobj) {
if (size()) {
if (typeid(back() != typeid(*newobj) ) throw inconsistent_ty pe();
}
list_base::push _back(smart_poi nter_type(newob j.release()));
}
};

Surely, there might be other ways to do it.
Jul 22 '05 #2
Siemel Naran wrote:
"cass" <ca******@knowh ouse.com> wrote in message news:mIHgc.5117 0

Both class A and B inherites from class C. A std::list is used to contain


a
list of objects of class A or a list of objects of class B, but not both
types of classes at the same time.

How to use template to realize this? Thanks!

If you use a list<C> you can hold both A and B objects in the same list.


I suppose you meant list<C*> here.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #3
"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:HwKgc.2081
Siemel Naran wrote:

If you use a list<C> you can hold both A and B objects in the same list.


I suppose you meant list<C*> here.


Yes. Maybe my wording was excellent, but in the next paragraph I wrote the
following:
Actually, in C++ you have to use something like
std::list<boo st::shared_ptr< C> > and ensure that C::~C() is virtual.


Raw pointers would have don't have auto destruct and deep copy, though we
could build that into our container, but what a hassle.

Jul 22 '05 #4
"Siemel Naran" <Si*********@RE MOVE.att.net> wrote in message
news:kzKgc.5202 4
Yes. Maybe my wording was excellent, but in the next paragraph I wrote the following:


Oops. Should be "Maybe my wording was *not* excellent"

Jul 22 '05 #5

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

Similar topics

9
3498
by: kazio | last post by:
Hello, So, I need to have double linked, circular list (last element point to the first one and first one points to the last one). I thought maybe I could use list container from STL, but unfortunately probably not. I checked it, and when I increase (++) iterator pointing to the last element the program crashes. I know the standard says, this is a linear list (with beginning and the end), but I completely don't understand why they...
7
1261
by: yilled_fred | last post by:
Hi can anyone tell me why the following code cannot/doesn't compile? I get the following error while compiling (g++ -c ): "In member function `void derivedclass<S>::foo()': test.h:20 error: parse error before `;' token Here is the code, am I missing something? #include <stdio.h> #include <stdlib.h>
9
2018
by: alopatenko | last post by:
I have a template class template <Class W> class WS At some point I have to use a STL list WS<W>objects so, I define #include <list>
1
3324
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one to use as an index to the freearray cells). Here is the exact problem specifications: Create an ordered list template class named OLType to implement an ordered list with operations of insert, remove, print, empty, full, size. The storage...
7
2968
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ Contents cn; list < BTree_node < Contents children; ........
6
3254
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to read my program I appriciate it. the program suppose to print a message on the screen #include <iostream> #include <string>
12
2695
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
2
1397
by: .rhavin grobert | last post by:
hello;-) i have that following little template that defines some type of vector. it works with structs and i want to use it also for simple pointers. the problem is, in following function... ______________________________________ template <class T, typename I>
7
5764
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
8402
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
8315
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
8829
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8508
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
5633
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
4164
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
2733
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
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
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.