 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
jean Guest
|
Posted: Sat Aug 12, 2006 4:30 am Post subject: STL variable scope |
|
|
Hi,
I have the following code(it may have syntax error), but ignore. Just
the idea. The question is:
1. Does customTagsEle get destroyed when func1() exit? It seems not.
But why not? local variable should be destroyed when it do out of
scope.
2.When does m_map get destroyed? Is it when CMyClass object get
destroyed?
3. When does the class A's object get destroyed?
Class A
{
....
}
class CMyClass
{
private:
std::map<std::string, boost::shared_ptr<A> > m_map;
public:
func1();
}
CMyClass::func1()
{
std::pair< std::string, boost::shared_ptr<A> > customTagsEle;
customTagsEle = std::make_pair( std::string((LPCTSTR)m_name),
boost::shared_ptr<A>( new A ) );
m_map.push_back( customTagsEle);
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sat Aug 12, 2006 4:58 pm Post subject: Re: STL variable scope |
|
|
{ Extra blank lines (double-spacing) removed. Please don't post
double-spaced articles. It's probably the posting tool doing this
behind the scenes; let's hope my e-mail client doesn't <g>. -mod/aps }
"jean" <jean.shu (AT) gmail (DOT) com> writes:
[rearranged by tm]
| Quote: | Class A
{
....
}
class CMyClass
{
private:
std::map<std::string, boost::shared_ptr<A> > m_map;
public:
func1();
}
CMyClass::func1()
{
std::pair< std::string, boost::shared_ptr<A> > customTagsEle;
customTagsEle = std::make_pair( std::string((LPCTSTR)m_name),
boost::shared_ptr<A>( new A ) );
m_map.push_back( customTagsEle);
}
1. Does customTagsEle get destroyed when func1() exit?
|
Yes.
| Quote: | It seems not. But why not? local variable should be destroyed when
it do out of scope.
|
How do you come to the conclusion that it doesn't?
| Quote: | 2.When does m_map get destroyed? Is it when CMyClass object get
destroyed?
|
Yes.
| Quote: | 3. When does the class A's object get destroyed?
|
Please post a complete, compilable program if you want to get a
accurate answer.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Alex Guest
|
Posted: Sat Aug 12, 2006 6:48 pm Post subject: Re: STL variable scope |
|
|
{ Quoted clc++m banner removed. -mod/aps }
jean wrote:
| Quote: | 1. Does customTagsEle get destroyed when func1() exit? It seems not.
But why not? local variable should be destroyed when it do out of
scope.
|
customTagsEle does get destroyed. However, m_map contains a copy of it.
| Quote: | 2.When does m_map get destroyed? Is it when CMyClass object get
destroyed?
|
Yes.
| Quote: | 3. When does the class A's object get destroyed?
|
On m_map destruction.
| Quote: |
Class A
{
....
}
class CMyClass
{
private:
std::map<std::string, boost::shared_ptr<A> > m_map;
public:
func1();
}
CMyClass::func1()
{
std::pair< std::string, boost::shared_ptr<A> > customTagsEle;
customTagsEle = std::make_pair( std::string((LPCTSTR)m_name),
boost::shared_ptr<A>( new A ) );
m_map.push_back( customTagsEle);
}
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Vladimir Marko Guest
|
Posted: Tue Aug 15, 2006 2:31 am Post subject: Re: STL variable scope |
|
|
jean wrote:
| Quote: | std::pair< std::string, boost::shared_ptr<A> > customTagsEle;
customTagsEle = std::make_pair( std::string((LPCTSTR)m_name),
boost::shared_ptr<A>( new A ) );
|
I'd like to point out that this line is breaks the basic exception
safety guarantee. The compiler is free to choose the following
order of "function calls":
1. new A
2. construct std::string
3. construct boost::shared_ptr<.>
4. std::make_pair(.,.)
If the construction of std::string throws, the A object is leaked.
Regards,
Vladimir Marko
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|