473,799 Members | 3,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL-container used with references

hi!

normally i would simply do the following:

std::vector<Ele ment> vec;
void somefunc() {
Element e;
vec.push_back(e );
}

now Element e is in the vector. thats fine as long as no polymorphic
behaviour is needed.

std::vector<Ele ment &> vec;
void somefunc() {
Derived_from_El ement e;
vec.push_back(e ); //bad idea,.. e is gone after functionscope is left
}

what i want to avoid (if possible) are 2 things:
dynamic allocation of the objects, and having an extracontiner for every
type derived from the basetype to store the elements.

i hope, i made clear what i'm trying to do... so is there a solution (except
for the 2 mentioned above?)

thx, regards,
sev
Jul 22 '05 #1
32 1793
On Tue, 6 Apr 2004 15:04:53 +0200, "Severin Ecker" <se****@gmx.a t>
wrote:
hi!

normally i would simply do the following:

std::vector<El ement> vec;
void somefunc() {
Element e;
vec.push_back(e );
}

now Element e is in the vector. thats fine as long as no polymorphic
behaviour is needed.

std::vector<El ement &> vec;
You can't hold references in containers - references are just aliases,
not real objects.
void somefunc() {
Derived_from_El ement e;
vec.push_back(e ); //bad idea,.. e is gone after functionscope is left
}

what i want to avoid (if possible) are 2 things:
dynamic allocation of the objects, and having an extracontiner for every
type derived from the basetype to store the elements.

i hope, i made clear what i'm trying to do... so is there a solution (except
for the 2 mentioned above?)


It is obviously hard to avoid dynamic allocation of the objects, since
all of your derived types can have different sizes and alignment
requirements. There are techniques for doing it (as long as all
derived classes are known in advance), but they would be premature
optimization in this case I am sure. Your best bet is to use a
container of smart pointers:

std::vector<sha red_ptr<Element > > vec;
vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));

See www.boost.org for shared_ptr.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
On Tue, 6 Apr 2004 15:04:53 +0200, "Severin Ecker" <se****@gmx.a t>
wrote:
hi!

normally i would simply do the following:

std::vector<El ement> vec;
void somefunc() {
Element e;
vec.push_back(e );
}

now Element e is in the vector. thats fine as long as no polymorphic
behaviour is needed.

std::vector<El ement &> vec;
You can't hold references in containers - references are just aliases,
not real objects.
void somefunc() {
Derived_from_El ement e;
vec.push_back(e ); //bad idea,.. e is gone after functionscope is left
}

what i want to avoid (if possible) are 2 things:
dynamic allocation of the objects, and having an extracontiner for every
type derived from the basetype to store the elements.

i hope, i made clear what i'm trying to do... so is there a solution (except
for the 2 mentioned above?)


It is obviously hard to avoid dynamic allocation of the objects, since
all of your derived types can have different sizes and alignment
requirements. There are techniques for doing it (as long as all
derived classes are known in advance), but they would be premature
optimization in this case I am sure. Your best bet is to use a
container of smart pointers:

std::vector<sha red_ptr<Element > > vec;
vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));

See www.boost.org for shared_ptr.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3

"tom_usenet " <to********@hot mail.com> wrote in message
...Your best bet is to use a
container of smart pointers:

std::vector<sha red_ptr<Element > > vec;
vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));

See www.boost.org for shared_ptr.


Just curious...What if you don't have (or want) boost? Is there an STL
solution similar to shared_ptr?

-Howard
Jul 22 '05 #4

"tom_usenet " <to********@hot mail.com> wrote in message
...Your best bet is to use a
container of smart pointers:

std::vector<sha red_ptr<Element > > vec;
vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));

See www.boost.org for shared_ptr.


Just curious...What if you don't have (or want) boost? Is there an STL
solution similar to shared_ptr?

-Howard
Jul 22 '05 #5

"Howard" <al*****@hotmai l.com> wrote in message
news:c4******** @dispatch.conce ntric.net...

"tom_usenet " <to********@hot mail.com> wrote in message
...Your best bet is to use a
container of smart pointers:

std::vector<sha red_ptr<Element > > vec;
vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));

See www.boost.org for shared_ptr.


Just curious...What if you don't have (or want) boost? Is there an STL
solution similar to shared_ptr?


No, but it's really very simple to roll your own basic smart pointer. It
would not have all the functionality of boost's but would certainly support
polymorphism and automatic cleanup.

Have a look at Scott Meyers book for example code (I think its the More
Effective C++ one).

john
Jul 22 '05 #6

"Howard" <al*****@hotmai l.com> wrote in message
news:c4******** @dispatch.conce ntric.net...

"tom_usenet " <to********@hot mail.com> wrote in message
...Your best bet is to use a
container of smart pointers:

std::vector<sha red_ptr<Element > > vec;
vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));

See www.boost.org for shared_ptr.


Just curious...What if you don't have (or want) boost? Is there an STL
solution similar to shared_ptr?


No, but it's really very simple to roll your own basic smart pointer. It
would not have all the functionality of boost's but would certainly support
polymorphism and automatic cleanup.

Have a look at Scott Meyers book for example code (I think its the More
Effective C++ one).

john
Jul 22 '05 #7
On 06 Apr 2004 11:14:31 EDT, "Howard" <al*****@hotmai l.com> wrote:

"tom_usenet " <to********@hot mail.com> wrote in message
...Your best bet is to use a
container of smart pointers:

std::vector<sha red_ptr<Element > > vec;
vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));

See www.boost.org for shared_ptr.


Just curious...What if you don't have (or want) boost? Is there an STL
solution similar to shared_ptr?


boost::shared_p tr has been proposed for standardization as part of the
library technical report. Look out for std::tr1::share d_ptr, coming to
your compiler soon.

There is no current standard solution, except to write your own smart
pointer class (not at all recommended - matching boost::shared_p tr's
functionality is non-trivial).

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #8
On 06 Apr 2004 11:14:31 EDT, "Howard" <al*****@hotmai l.com> wrote:

"tom_usenet " <to********@hot mail.com> wrote in message
...Your best bet is to use a
container of smart pointers:

std::vector<sha red_ptr<Element > > vec;
vec.push_back(s hared_ptr<Eleme nt>(new Derived_from_El ement));

See www.boost.org for shared_ptr.


Just curious...What if you don't have (or want) boost? Is there an STL
solution similar to shared_ptr?


boost::shared_p tr has been proposed for standardization as part of the
library technical report. Look out for std::tr1::share d_ptr, coming to
your compiler soon.

There is no current standard solution, except to write your own smart
pointer class (not at all recommended - matching boost::shared_p tr's
functionality is non-trivial).

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #9
"tom_usenet " <to********@hot mail.com> wrote in message
news:ne******** *************** *********@4ax.c om...
Just curious...What if you don't have (or want) boost? Is there an STL
solution similar to shared_ptr?


boost::shared_p tr has been proposed for standardization as part of the
library technical report. Look out for std::tr1::share d_ptr, coming to
your compiler soon.


Curiosity:

What's the significance of the (namespace?) name 'tr1'?

-Mike

Jul 22 '05 #10

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

Similar topics

2
4797
by: Jean-Baptiste | last post by:
Hi, I am currently porting a C++ project (linux version) under windows. This project uses the STL stuff. When I try to compile and built my project, I get a lot of errors. The main part of them seems to be due to the stl library. Indeed, it seems that the standard libary redefines some structure already defined in xutility. I tryed some tricks without any success.
5
2104
by: Scott Brady Drummonds | last post by:
Hi, everyone, A coworker and I have been pondering a memory allocation problem that we're having with a very large process. Our joint research has led us to the conclusion that we may have to replace the STL allocator. But, before I even attempt something like this, I wanted to ask some questions to this group: 1) We read online that STL caches allocated memory so that containers and their contents that are freed at one point in...
5
4238
by: Khalid | last post by:
II am allocating alot of memory for my problem model which uses stl containers for these pointers, will stl free the memory? by other words what is the semantics of memory ownership in stl? thanks a lot kh
11
2575
by: RR | last post by:
I have Plauger's book on STL and I've found that it's different from the STL I have on a Linux box (libstdc++-2.96-112). In particular, the map::erase(iterator) method I have returns nothing (void), not "iterator". So, does the latest C++ Standards document (INCITS/ISO/IEC 14882-2003) contain the definitive current standard for STL and all its templates? Is that the recommended document to find the correct STL semantics, or is
0
1473
by: Tony Johansson | last post by:
Hello! I have two classes called Handle which is a template class and a class Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some methods. I don't show the Integer class because it will not add any information to my problem. Main is using some STL function. In main you put in nodes in the STL list and you can display the STL list and other things such as erase some value from...
0
1868
by: rajd99 | last post by:
Hi, I am getting the following error while compiling with sun C++ 5.5 and using SGI STL. Has anyone run into this. The SGI headers are one at http://www.sgi.com/tech/stl/download.html Thanks, #include <stl/string> class ASPString : public stl::string { public:
32
2007
by: Conrad Weyns | last post by:
I have recently gone back to doing some work in c, after years of c++, but I find the lack of templates and in particular the container in the stl to be a huge show stopper. There are math libs, plotting libs, graphic libs etc but I what about some usefull container lib. Any ideas anyone? regards, Conrad Weyns
7
1993
by: rodrigostrauss | last post by:
I'm using Visual Studio 2005 Professional, and I didn't find the STL.NET. This code: #include "stdafx.h" #include <vector> using namespace System; using namespace std; int main(array<System::String ^> ^args)
15
2035
by: Herby | last post by:
This is a follow on from my previous thread about clr/safe and STL.NET http://groups.google.com/group/microsoft.public.dotnet.languages.vc/browse_thread/thread/0eb9b25a83bc02df/b170a89dbc67c332#b170a89dbc67c332 A more specific problem now is migrating the current MFC serialization onto STL serialization? How best to serilize using STL? STL does not seem to directly support the concept of serialization in
20
1839
by: Andrew Roberts | last post by:
Any more info on this? Anyone know when it will be released?
0
9687
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
9543
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
10488
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
10237
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
10029
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7567
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
5467
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...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2941
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.