?
=?ISO-8859-1?Q?Xavier_D=E9coret?=
Hi,
I long thought one could not delete a const pointer but the following
code compiles under g++ and visual net:
class Dummy
{
public:
Dummy() {}
~Dummy() {}
};
int main(int,char**)
{
const Dummy* const p = new Dummy();
delete p;
return 0;
}
The question is: how can I forbid the user to delete some data I an
handling a pointer to? Here is an example of code that does compile but
will lead to segfault. How can I prevent it?
#include <iostream>
#include <string.h>
using namespace std;
class Invoice
{
public:
Invoice(char* label)
{
_label = strdup(label);
}
~Invoice()
{
delete [] _label;
}
const char* const label() const
{
return _label;
}
private:
char* _label;
};
int main(int,char**)
{
Invoice d("train");
delete [] d.label();
cout<<d.label()<<endl;
return 0;
}
I long thought one could not delete a const pointer but the following
code compiles under g++ and visual net:
class Dummy
{
public:
Dummy() {}
~Dummy() {}
};
int main(int,char**)
{
const Dummy* const p = new Dummy();
delete p;
return 0;
}
The question is: how can I forbid the user to delete some data I an
handling a pointer to? Here is an example of code that does compile but
will lead to segfault. How can I prevent it?
#include <iostream>
#include <string.h>
using namespace std;
class Invoice
{
public:
Invoice(char* label)
{
_label = strdup(label);
}
~Invoice()
{
delete [] _label;
}
const char* const label() const
{
return _label;
}
private:
char* _label;
};
int main(int,char**)
{
Invoice d("train");
delete [] d.label();
cout<<d.label()<<endl;
return 0;
}