473,508 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL list

class base
{
};

class d1 : public base
{
};

class d2 : public base
{
};

Then a bunch of objects instantiated from either d1 or d2.

Is it okay to put those objects into the same STL "list" container?

Thanks!
Jul 22 '05 #1
4 1673
Asapi wrote:
class base
{
};

class d1 : public base
{
};

class d2 : public base
{
};

Then a bunch of objects instantiated from either d1 or d2.

Is it okay to put those objects into the same STL "list" container?

Thanks!


No, it's not ok to put those objects in the same list, because you'll get the d1's and the d2's
(and their children) truncated to base's.

HOWEVER.... It's OK to store POINTERS to those objects into the same std::list. DISCLAIMER for the
gurus: Use your favorite shared_ptr implementation (probably boost) rather than raw pointers.

Jul 22 '05 #2
Asapi wrote:
class base {
};

class d1 : public base {
};

class d2 : public base {
};

Then a bunch of objects instantiated from either d1 or d2.

Is it okay to put those objects into the same STL "list" container?


No, but you can place base* pointers to them in a list<base*> container.

Jul 22 '05 #3

Asapi wrote:
class base
{
};

class d1 : public base
{
};

class d2 : public base
{
};

Then a bunch of objects instantiated from either d1 or d2.

Is it okay to put those objects into the same STL "list" container?

Thanks!
No, it's not ok to put those objects in the same list, because you'll get

the d1's and the d2's (and their children) truncated to base's.

HOWEVER.... It's OK to store POINTERS to those objects into the same std::list. DISCLAIMER for the gurus: Use your favorite shared_ptr implementation (probably boost) rather than raw pointers.


But do not use std::auto_ptr. I'm sure the gurus already knew that.
Jul 22 '05 #4
Walter Kalata wrote:
Asapi wrote:
class base
{
};

class d1 : public base
{
};

class d2 : public base
{
};

Then a bunch of objects instantiated from either d1 or d2.

Is it okay to put those objects into the same STL "list" container?

Thanks!


No, it's not ok to put those objects in the same list, because you'll get


the d1's and the d2's
(and their children) truncated to base's.

HOWEVER.... It's OK to store POINTERS to those objects into the same


std::list. DISCLAIMER for the
gurus: Use your favorite shared_ptr implementation (probably boost)


rather than raw pointers.
But do not use std::auto_ptr. I'm sure the gurus already knew that.


Whoops, you're absolutely right, I forgot to put that disclaimer in as well. However, technically, auto_ptr is not shared,
since it hands off ownership on any copy/assignment. But let's not get into that nitpicky a debate here --
there are plenty of other nitpicky debates around :-P

Jul 22 '05 #5

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

Similar topics

6
3070
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesnąt matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone...
10
15099
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
24
5716
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
4
3585
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
3
2698
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
0
1808
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
10
6553
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
8611
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
12
3998
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
7
5756
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...
0
7118
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...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
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,...
0
4706
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...
0
3192
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...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
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 ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
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...

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.