STL::List的对象存储与释放

本文涉及的产品
对象存储 OSS,20GB 3个月
对象存储 OSS,恶意文件检测 1000次 1年
对象存储 OSS,内容安全 1000次 1年
简介: 写了一个小程序,来检查一下List中如何管理存储的对象。 #include "stdafx.h"#include #include using namespace std; class MyClass{  public:  int nMemb...

写了一个小程序,来检查一下List中如何管理存储的对象。

#include "stdafx.h"
#include <list>
#include <iostream>

using namespace std;

class MyClass
{
  public:
  int nMember;
  int * pMember;
  public:
   
    MyClass(void)
 {
  cout<<"New my Class"<<endl;
  nMember = 0;
  pMember = &nMember;
  cout<<" pMember is "<<pMember<<endl;
 };

 ~MyClass(void)
 {
  cout<<"Free my Class"<<endl;
  cout<<" The number is "<<nMember<<endl;
  cout<<" pMember is "<<pMember<<endl;
  cout<<" *pMember is "<<*pMember<<endl;
  *pMember = 15;
 }
};


int _tmain(int argc, _TCHAR* argv[])
{
 list<MyClass> m_Container;
 
 MyClass *a = new MyClass();

 a->nMember = 1;

 m_Container.push_back(*a);

 a->nMember = 2;
 delete a;

 return 0;
}

 

程序的运行结果如下:

STL::List的对象存储与释放
显然当调用push_back时,生成了一个新的实例,所以出现了两个析构函数的调用。同时观察一下pMember值的变化,这可是很容易出问题的地方!

 

如果List存储的是对象指针可就要小心了。

相关实践学习
借助OSS搭建在线教育视频课程分享网站
本教程介绍如何基于云服务器ECS和对象存储OSS,搭建一个在线教育视频课程分享网站。
目录
相关文章
|
10天前
|
存储 缓存 编译器
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
|
10天前
|
算法 C++ 容器
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
|
1月前
|
存储 C语言 C++
C++中STL常用容器(vector、deque、list、map、set)一文带你了解
C++中STL常用容器(vector、deque、list、map、set)一文带你了解
|
2月前
|
编译器 C++ 容器
STL常用之vector,list,stack,queue,deque总结与对比
STL常用之vector,list,stack,queue,deque总结与对比
|
2月前
|
存储 C++
C++STL模板之——list(简化源码,模拟源码)
C++STL模板之——list(简化源码,模拟源码)
|
2月前
|
存储 C++ 容器
【C++修行之道】STL(初识list、stack)
【C++修行之道】STL(初识list、stack)
|
4月前
|
算法 C++ 容器
【C++STL基础入门】list改、查操作
【C++STL基础入门】list改、查操作
463 0
|
4月前
|
算法 C++ 容器
【C++STL基础入门】list的增、删
【C++STL基础入门】list的增、删
|
4月前
|
存储 算法 C++
【C++STL基础入门】list基本使用
【C++STL基础入门】list基本使用
|
4月前
|
C++ 容器
【C++STL基础入门】list交换、翻转,排序、合并和拼接操作
【C++STL基础入门】list交换、翻转,排序、合并和拼接操作