vector类型静态成员变量 overflow的问题

#include
#include
#include
using namespace std;

class CMyClass
{
public:
CMyClass(){}
CMyClass(int a)
{
this->m_nValue = a;
vec.push_back(*this);
}
~CMyClass()
{
/*不知道怎么指向当前类*/
vector::iterator it;
for (it = vec.begin(); it != vec.end(); it++)
{
if (it->vec == *this)
vec.erase(it);

    }

    //int length = vec.size();
    //for (int i = 0; i < length; i++)
    //{
    //  if (&vec[i] == this)
    //  {
    //      CMyClass temp = vec[i];
    //      
    //  }

/*  }*/
    //for (it=vec.begin(); it != vec.end(); it++)
    //{
    //  if (vec[])
    //  vec.erase(it);
    //}
    //cout << "delete d" << endl;

}
friend bool operator==(vector<CMyClass>v, CMyClass& c2)
{
    int length = v.size();
    for (int i = 0; i < length; i++)
    {
        if (&v[i] == &c2)
            return true;
    }
    return false;
}
friend ostream& operator<<(ostream& out, CMyClass& c)
{
    out << c.m_nValue << endl;
    return out;
}
static void ShowList()
{
    vector<CMyClass>::iterator it;
    for (it = vec.begin(); it != vec.end(); it++)
    {
        cout << *it;

    }
}

private:
int m_nValue;
static vectorvec;
};

vectorCMyClass::vec;

int main()
{
CMyClass a(100);
CMyClass b(1);
CMyClass c;
CMyClass* d = new CMyClass;
*d = 10;
cout << "The first end:" << endl;
CMyClass::ShowList();
if (1)
{
CMyClass e(1000);
cout << "The second end:" << endl;
CMyClass::ShowList();
}
delete d;
cout << "The third end:" << endl;
CMyClass::ShowList();
return 0;
}
类里的vector类型静态成员变量,目的是创建新对象时把它加入到容器中,析构时反之 现在出现overflow的问题

就是erase的问题,你需要用it = erase的返回值

你的代码要想能运行 需重写拷贝构造函数 和 赋值函数

friend bool operator==(vectorv, CMyClass& c2)
{
int length = v.size();
for (int i = 0; i < length; i++)
{
if (&v[i] == &c2)
return true;
}
return false;
}
上面函数逻辑有问题 任何情况下都为真

#include "stdafx.h"
#include
#include
using namespace std;
class CMyClass
{
public:
CMyClass()
{
this->m_nValue = 0;
vec.push_back(this);
}
CMyClass(int a)
{
this->m_nValue = a;
vec.push_back(this);
}
~CMyClass()
{
/*不知道怎么指向当前类*/
vector< CMyClass* >::iterator it;
for (it = vec.begin(); it != vec.end(); it++)
{
if (*it == this)
{
it = vec.erase(it);
break;
}
}
}

friend bool operator==(vector< CMyClass* >v, CMyClass& c2)
{
    int length = v.size();
    for (int i = 0; i < length; i++)
    {
        if (v[i] == &c2)
            return true;
    }
    return false;
}

friend ostream& operator<<(ostream& out, CMyClass& c)
{
    out << c.m_nValue << endl;
    return out;
}
static void ShowList()
{
    vector< CMyClass* >::iterator it;
    for (it = vec.begin(); it != vec.end(); it++)
    {
        cout << (*it)->m_nValue<<endl;

    }
}

private:
int m_nValue;
static vector< CMyClass* > vec;
};

vector< CMyClass* > CMyClass::vec;

int _tmain(int argc, _TCHAR* argv[])
{
CMyClass a(100);
CMyClass b(1);
CMyClass c;
CMyClass* d = new CMyClass(10);
// *d = 10;
cout << "The first end:" << endl;
CMyClass::ShowList();
if (1)
{
CMyClass e(1000);
cout << "The second end:" << endl;
CMyClass::ShowList();
}
delete d;
cout << "The third end:" << endl;
CMyClass::ShowList();
system("pause");
return 0;
}

for (it = vec.begin(); it != vec.end(); it++)
{
if (it->vec == *this)
vec.erase(it);
}
这块要这么写
for (it = vec.begin(); it != vec.end(); )
{
if (it->vec == *this)
it = vec.erase(it);
else
it++;
}