need some explanation of the following scope problem

I am reading a book about auto_ptr, and see the following code:

1
2
3
4
5
6
7
8
auto_ptr<string> films[5] =
{
    auto_ptr<string> (new string("Fowl Balls")),
    auto_ptr<string> (new string("Duck Walks")),
    auto_ptr<string> (new string("Chicken Runs")),
    auto_ptr<string> (new string("Turkey Errors")),
    auto_ptr<string> (new string("Goose Eggs"))
};


I am wondering if the scope of the auto_ptr is local to the declaration of films array and the destructor will be executed after the declaration of the array, I think I am confused here. need some help to explain the scope and if this declaration can be successful.
Thanks.
Each element of the initializer list is a temporary unnamed object which will be deleted in reverse order after the initialization of the array.
does it mean I can not really use the array "film" as after the initialization, the destructor will be called
1
2
3
4
5
6
7
8
9
10
11
12
13
{
    auto_ptr<string> films[5] =
    {
        auto_ptr<string> (new string("Fowl Balls")),
        auto_ptr<string> (new string("Duck Walks")),
        auto_ptr<string> (new string("Chicken Runs")),
        auto_ptr<string> (new string("Turkey Errors")),
        auto_ptr<string> (new string("Goose Eggs"))
    };
    
    // the objects in the films no longer exist here ??  

}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <memory>
using namespace std;

struct VerboseObject
{
	std::string desc ;

	VerboseObject( string description ) 
		: desc(description) { cout << desc << " constructed!\n" ;}

	~VerboseObject()
	{
		cout << desc << " destructed!\n" ;
	}

};

int main()
{
	{
		cout << "--- inner scope\n" ;
		auto_ptr<VerboseObject> films[5] =
		{
			auto_ptr<VerboseObject> (new VerboseObject("Fowl Balls")),
			auto_ptr<VerboseObject> (new VerboseObject("Duck Walks")),
			auto_ptr<VerboseObject> (new VerboseObject("Chicken Runs")),
			auto_ptr<VerboseObject> (new VerboseObject("Turkey Errors")),
			auto_ptr<VerboseObject> (new VerboseObject("Goose Eggs"))
		};

		cout << "--- leaving inner scoope\n\n" ;
	}
	cout << "\n\nout of inner scope\n" ;
}
--- inner scope
Fowl Balls constructed!
Duck Walks constructed!
Chicken Runs constructed!
Turkey Errors constructed!
Goose Eggs constructed!
--- leaving inner scoope

Goose Eggs destructed!
Turkey Errors destructed!
Chicken Runs destructed!
Duck Walks destructed!
Fowl Balls destructed!


out of inner scope


No temporaries here, in the same way that:

auto_ptr<string> ptr = auto_ptr<string>(new string ("FowlBalls")) ;

doesn't create a temporary.

Nevertheless, you should prefer unique_ptr if it's available to you.
Last edited on
Thanks, it helps to convince me that array {} does not mean scope? Can you give me some more insights about why?
<type> a[num] =
{ #This is not scope??

};


When you define a type and use the = { }; syntax, the {} denote the beginning and end of an initialization list, not a block of code.
Last edited on
Topic archived. No new replies allowed.