473,385 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1666
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
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
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.