why does this program have an exception in VC++ 2008 Express?
#include <conio.h>
//#include <C:\WString(v2.00).h>
#include <iostream>
#include <string>
using namespace std;
class WCS
{
public:
wchar_t* string;
WCS(const wstring& wstr)
{
wchar_t* c = new wchar_t [wstr.size() + 1];
wcscpy(c,wstr.c_str());
string = c;
}
~WCS()
{
delete [] string;
}
operator wchar_t*&()
{
return string;
}
};
int main()
{
wstring wstr(L"hello");
wchar_t* const& c = WCS(wstr);
wcout << c;
_getch();
}
I tested it in a borland compiler & there was no exception. Perhaps there's a problem with VC++!
First-chance exception at 0x102670ef in Console.exe: 0xC0000005: Access violation reading location 0x00037000.
Unhandled exception at 0x102670ef in Console.exe: 0xC0000005: Access violation reading location 0x00037000.