auto-ptr

Compilation problems with vector<auto_ptr<> >

你说的曾经没有我的故事 提交于 2019-12-29 07:40:07
问题 Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct A { int a; A(int a_):a(a_) {} }; int main() { vector<auto_ptr<A> > as; for (int i = 0; i < 10; i++) { auto_ptr<A> a(new A(i)); as.push_back(a); } for (vector<auto_ptr<A> >::iterator it = as.begin(); it != as.end(); ++it) cout << (*it)->a << endl; } When trying to compile it, I get the following obscure compiler error from g++: g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF

Cast auto_ptr<Base> to auto_ptr<Derived>

你说的曾经没有我的故事 提交于 2019-12-25 00:46:24
问题 Please help me to understand the following issue. Look at the code example below: #include <iostream> class Shape { public: virtual wchar_t *GetName() { return L"Shape"; } }; class Circle: public Shape { public: wchar_t *GetName() { return L"Circle"; } double GetRadius() { return 100.; } }; int wmain() { using namespace std; auto_ptr<Shape> aS; auto_ptr<Circle> aC(new Circle); aS = aC; wcout << aS->GetName() << L'\t' << static_cast<auto_ptr<Circle>>(aS)->GetRadius() << endl; return 0; } Why I

Why vector.push_back(auto_ptr) wouldn't compile?

混江龙づ霸主 提交于 2019-12-22 05:43:15
问题 I learned that STL can forbid programmer putting an auto_ptr into a container. For example following code wouldn't compile: auto_ptr<int> a(new int(10)); vector<auto_ptr<int> > v; v.push_back(a); auto_ptr has the copy constructor, why this code can even compile? 回答1: Looking at the definition of std::auto_ptr: namespace std { template <class Y> struct auto_ptr_ref {}; template <class X> class auto_ptr { public: typedef X element_type; // 20.4.5.1 construct/copy/destroy: explicit auto_ptr(X* p

What's the best way to return something like a collection of `std::auto_ptr`s in C++03?

℡╲_俬逩灬. 提交于 2019-12-22 04:48:13
问题 std::auto_ptr is not allowed to be stored in an STL container, such as std::vector . However, occasionally there are cases where I need to return a collection of polymorphic objects, and therefore I can't return a vector of objects (due to the slicing problem). I can use std::tr1::shared_ptr and stick those in the vector , but then I have to pay a high price of maintaining separate reference counts, and object that owns the actual memory (the container) no longer logically "owns" the objects

std::vector of object containing auto_ptr behaves strangely

最后都变了- 提交于 2019-12-13 23:47:07
问题 I want to create a control panel for my application using Qt library and for this i create class Controls class Controls : create a slider and spin and a label and orgnize them in horizontal layout . but when i want to create std::vector<Controls> the program run without error but now controls is creating at all ! so why there is no controls. Controls.h class Controls { private: QHBoxLayout Layout ; string Controlname; std::auto_ptr<QLabel> Label ; std::auto_ptr<QSlider> Slider ; std::auto

What is the size of an auto_ptr?

Deadly 提交于 2019-12-12 03:09:39
问题 Does an auto_ptr have the same size as a pointer? I have to substitute it with a boost::scoped_ptr , and I was wondering if these two data types have the same size. 回答1: You can pretty easily find out what the sizes are by simply doing this: #include <iostream> #include <memory> #include <boost/shared_ptr.hpp> #include <boost/scoped_ptr.hpp> int main() { std::cout << "raw pointer: " << sizeof(int*) << std::endl; std::cout << "auto-ptr: " << sizeof(std::auto_ptr<int>) << std::endl; std::cout <

Can't inherit from auto_ptr without problems

血红的双手。 提交于 2019-12-12 01:31:38
问题 What I want to do is this: #include <memory> class autostr : public std::auto_ptr<char> { public: autostr(char *a) : std::auto_ptr<char>(a) {} autostr(autostr &a) : std::auto_ptr<char>(a) {} // define a bunch of string utils here... }; autostr test(char a) { return autostr(new char(a)); } void main(int args, char **arg) { autostr asd = test('b'); return 0; } (I actually have a copy of the auto_ptr class that handles arrays as well, but the same error applies to the stl one) The compile error

Making a non-object resource RAII-compliant

孤街浪徒 提交于 2019-12-11 12:31:28
问题 in my code I use HANDLE s from windows.h . They are used like HANDLE h; if (!openHandleToSomething(arg1, arg2, &h)) { throw std::exception("openHandleToSomething error"); } /* Use the handle in other functions which can throw as well */ if (!CloseHandle(h)) { throw std::exception("closeHandle error"); } As you see, you have to insert this CloseHandle to every exception which can happen in the middle of acquiration and release. Therefore, it's likely you forget one (or there is a fancy SEH

'auto_ptr' and STL containers: writing an example of erroneous usage

假如想象 提交于 2019-12-11 02:05:17
问题 This question raised after reading this tutorial: http://www.cprogramming.com/tutorial/auto_ptr.html There you can find the following statement: A subtle consequence of this behavior is that auto _ ptrs don't work well in all scenarios. For instance, using auto _ ptr objects with the standard template library can lead to problems as some functions in the STL may make copies of the objects in containers such as the vector container class. One example is the sort function, which makes copies of

How do you assign a returned auto_ptr?

家住魔仙堡 提交于 2019-12-11 01:18:05
问题 I'm trying to learn auto_ptr, so I wrote the code below but it results with ..\src\main.cpp:23: error: no match for 'operator=' in 'p1 = source()()' What have I done wrong? How do you assign a returned auto_ptr? #include <stdio.h> #include <memory> using namespace std; auto_ptr<int> source() { int *i = new int(); *i = 100; return auto_ptr<int>(i); } int main() { std::auto_ptr<int> p1, p2; p1 = p2; p1 = source(); return 0; } 回答1: You cannot. auto_ptr is a fundamentally broken class. You must