473,666 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

auto_ptr vector discards qualifier

Hi all,

I'm trying to create a vector of pointers to a class:

For example:

class CMb ...

std::vector< CMb* > myvect(100);

myvect[10] = new CMb();

But this doesn't work. g++ just spews out a whole load of uninituitive
errors around the assignment operation.

So, have just read a post on this newsgroup from a few years ago, I'm
trying to use an auto_ptr instead.

so,

std::vector< std::auto_ptr<C Mb> > myvect(100);
myvect[10] = new CMb();

But, this time the compiler complains that passing const
std::auto_ptr<C Mb> discards a qualifier.

How to I fix this?

Thanks,

Stephen
Jul 22 '05 #1
2 2344
"stephen henry" <st******@yahoo .com> wrote in message
news:84******** *************** ***@posting.goo gle.com...
I'm trying to create a vector of pointers to a class:

For example:

class CMb ...

std::vector< CMb* > myvect(100);

myvect[10] = new CMb();

But this doesn't work. g++ just spews out a whole load of uninituitive
errors around the assignment operation.
The following code sample compiles without problem:
#include <vector>
class CMb {};

int main()
{
std::vector< CMb* > myvect(100);
myvect[10] = new CMb;
}

What was the error you were encountering?
So, have just read a post on this newsgroup from a few years ago, I'm
trying to use an auto_ptr instead.

so,

std::vector< std::auto_ptr<C Mb> > myvect(100);
myvect[10] = new CMb();

But, this time the compiler complains that passing const
std::auto_ptr<C Mb> discards a qualifier.


It is illegal to use a container of std::auto_ptr
(because instances of auto_ptr cannot properly be copied).
You could use another smart pointer instead, however,
such as boost::shared_p tr (from www.boost.org), which
will be adopted in the next revision of the standard.
hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2

"stephen henry" <st******@yahoo .com> schrieb im Newsbeitrag
news:84******** *************** ***@posting.goo gle.com...

class CMb ...

std::vector< CMb* > myvect(100);

myvect[10] = new CMb();
with a dummy class "class CMb {};" the above code compiles on my gcc 3.3.4.
and so does my VC7 13.10.3052.


std::vector< std::auto_ptr<C Mb> > myvect(100);
myvect[10] = new CMb();


I can hardly believe that using std::auto_ptr with a std::vector is safe as
std::auto_ptr does not have
value semantics as recommended for std::vector.
(The implementation of std::vector could use temporary copies, which would
discard all your pointers)

Best Regards
Michael
Jul 22 '05 #3

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

Similar topics

3
17128
by: SerGioGio | last post by:
Hello ! When a ref. counted smart pointer (template) class is available (like boost::shared_ptr), is there any (semantic) reason to still use std::auto_ptr rather than this smart pointer ? Or should one use shared_ptr for every cases ? Thanks in advance for your remarks ! SerGioGio
1
2496
by: bucher | last post by:
Hi, I want to push a const auto_ptr into a vector, but the compile reports errors. Below is the code. class Folder; class Result; class Results { public: int size(){return _Items.size();}
18
679
by: puzzlecracker | last post by:
I am using pointers in my program and they obviously make me a bit nervious about possible program states. I am giving serious thoughs about using auto_ptr but unsure whether they can be placed in vectors. This is approximaton of where I need to use them /currently class A { //some members virtual ~A();
6
95828
by: Jason | last post by:
I have a function (Inet_ntop) that returns const char * and if I try to assign that return value to a char * variable, I get the gcc error message: warning: assignment discards qualifiers from pointer target type Does anyone know what this warning means? Why do I get it? The program compiles and appears to work, but I'd like to understand the warning. Here is basically what the code looks like: char *str;
8
1156
by: Lloyd Dupont | last post by:
I try to use the auto_ptr in my code but that doesn't work. even std::auto_ptr. I guess I have to add an #include statement, but I can't figure out the right file to add. Also I want to use auto_ptr not to manage a single int but and int array. something like std::auto_ptr<int> adv = new int; but from what I read auto_ptr doesn't seems to be the right solution for the
14
3486
by: Andrew | last post by:
Hello all: After spending some time figuring out auto_ptr class' implementation, I decided to write a small article detailing its use of the auto_ptr_ref proxy class to enable construction and assignment from temporaries. I have placed the manuscript at: http://www.nd.edu/~ahenrick/auto_ptr.pdf It develops the problem and its solution in an incremental manner, which will hopefully make it accessible for most people without being
13
2083
by: Shark | last post by:
Hi, I was reading on http://www.kuro5hin.org/story/2005/10/31/53857/831 that auto_ptr uses "delete" in its destructor instead of delete. The article is fairly recent, so I was wondering if this is true in general for any hosted implementation.
2
2960
by: Markus Dehmann | last post by:
I am trying to make a pair with a string and an auto_ptr: #include <iostream> #include <map> using namespace std; int main(){ auto_ptr<intp(new int(3)); make_pair("x",p); }
22
3165
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my header it looks like this (private section) vector<Directory*Directories;
0
8352
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
8863
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...
0
8780
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...
0
8636
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
6189
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
4192
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
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2005
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1763
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.