A
A_StClaire_
I read a section of my text on smart "counting" pointers and found it
confusing so I decided to get hands-on.
however I'm getting "Debug Assertion Failed... Expression:
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" whenever I run my code below.
program works fine after I bypass the msg.
any ideas? thx a lot.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace std;
template <class T> class CountedPtr
{
public:
T* ptr;
long* count;
void dispose()
{
if((--(*count)) == 0)
{
delete count;
delete ptr;
}
}
explicit CountedPtr(T* p = 0) : ptr(p), count(new long(1))
{
}
CountedPtr(const CountedPtr <T>& p) throw() : ptr(p.ptr),
count(p.count)
{
++(*count);
}
~CountedPtr() throw()
{
dispose();
}
CountedPtr <T>& operator= (const CountedPtr <T>& p) throw()
{
if(this != &p)
{
dispose();
ptr = p.tr;
count = p.count;
++(*count);
}
return *this;
}
T& operator*() const throw()
{
return *ptr;
}
T* operator->() const throw()
{
return ptr;
}
};
int _tmain()
{
int i = 10;
int* sampleIntPtr = &i;
cout << *sampleIntPtr << "\n\n";
CountedPtr <int> countedIntPtr(sampleIntPtr);
CountedPtr <int> countedIntPtr2 = countedIntPtr;
cout << *countedIntPtr << "\n\n";
cout << *countedIntPtr2 << "\n\n";
cout << *(countedIntPtr.count) << "\n\n";
cout << *(countedIntPtr2.count) << "\n\n";
CountedPtr <int> countedIntPtr3 = countedIntPtr2;
CountedPtr <int> countedIntPtr4 = countedIntPtr2;
cout << *(countedIntPtr.count) << "\n\n";
return 0;
}
confusing so I decided to get hands-on.
however I'm getting "Debug Assertion Failed... Expression:
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" whenever I run my code below.
program works fine after I bypass the msg.
any ideas? thx a lot.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace std;
template <class T> class CountedPtr
{
public:
T* ptr;
long* count;
void dispose()
{
if((--(*count)) == 0)
{
delete count;
delete ptr;
}
}
explicit CountedPtr(T* p = 0) : ptr(p), count(new long(1))
{
}
CountedPtr(const CountedPtr <T>& p) throw() : ptr(p.ptr),
count(p.count)
{
++(*count);
}
~CountedPtr() throw()
{
dispose();
}
CountedPtr <T>& operator= (const CountedPtr <T>& p) throw()
{
if(this != &p)
{
dispose();
ptr = p.tr;
count = p.count;
++(*count);
}
return *this;
}
T& operator*() const throw()
{
return *ptr;
}
T* operator->() const throw()
{
return ptr;
}
};
int _tmain()
{
int i = 10;
int* sampleIntPtr = &i;
cout << *sampleIntPtr << "\n\n";
CountedPtr <int> countedIntPtr(sampleIntPtr);
CountedPtr <int> countedIntPtr2 = countedIntPtr;
cout << *countedIntPtr << "\n\n";
cout << *countedIntPtr2 << "\n\n";
cout << *(countedIntPtr.count) << "\n\n";
cout << *(countedIntPtr2.count) << "\n\n";
CountedPtr <int> countedIntPtr3 = countedIntPtr2;
CountedPtr <int> countedIntPtr4 = countedIntPtr2;
cout << *(countedIntPtr.count) << "\n\n";
return 0;
}