Firstly you have to forget the C-prefix - its ugly and its not part of the ungary notation how the most of people think.
Now to your problem. You only make a shallow copy here: pTemp[i] = m; like memcpy. So when m gets out of scope the destructor of CMain gets called. The destructor now deletes the array you allocated before in the consturctor, the result of that is, that pK gets invalid in pTemp[i].
EDIT:
This isn't enought:
Replacing pTemp[i] = m with pTemp[i] = std::move(m) should solve it.
You have to define your own move assignment operator, the full working program:
#include <vector>
#include <cstring>
class CMain
{
public:
CMain(int nDim)
{
dim = nDim;
pK = new int[nDim];
memset(pK, 0, sizeof(int) * nDim);
}
~CMain()
{
if (pK != NULL)
{
delete[] pK;
pK = NULL;
}
}
CMain& operator() (CMain& other)
{
dim = other.dim;
delete[] pK;
pK = new int[dim];
memcpy(pK, other.pK, sizeof(int) * dim);
return *this;
}
CMain& operator=(CMain&& other)
{
dim = other.dim;
pK = other.pK;
other.pK = nullptr;
return *this;
}
int dim;
int* pK;
};
int main(int argc, char* argv[])
{
std::vector<CMain> pTemp(3, CMain(3));
for (int i = 0; i < 3; i++)
{
CMain m(1);
m.pK[0] = i;
m.pK[1] = i;
m.pK[2] = i;
pTemp[i] = std::move(m);
}
CMain mm(3);
for (int i = 0; i < 3; i++)
{
if (pTemp[i].pK[1] == 2)
mm = std::move(pTemp[0]);
}
return 0;
}
Or if you need multyple pointer to the same object either your store the ptr in a globaly object preferably instanced as normaly object and point to them from your CMain-Objects or you use an std::shared_ptr<int[]> with an custom destructor.
C++ is hard to learn^^