|
-
July 29th, 1999, 03:00 AM
#1
How to protect object passed by const reference from destruction ?
Hi folks,
In the following code, I have passed an object by reference to a function. I have attempted to protect the object by using the const parameter. But I can still destroy the object (through a pointer to it). How do I protect against this ? I know the code is strange - but it is valid for the vehicle class to wish to have a data member (ptrperson) pointing to the incoming object.
class person
{
public :
int age;
};
class vehicle
{
public :
person *ptrperson;
void destroy(const person & refperson);
};
void vehicle: estroy(const person& refperson)
{
*ptrperson = refperson;
ptrperson->age++; // I don't like this - but it's legal !
delete ptrperson; // I like this evne less - but it too is legal !
}
void main (void)
{
person me;
vehicle car;
car.destroy(me);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|