Click to See Complete Forum and Search --> : How to protect object passed by const reference from destruction ?


Brendan Cullen
July 29th, 1999, 03:00 AM
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::destroy(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);
}

Burlacu Ovidiu
July 29th, 1999, 03:17 AM
I have 2 things unclear:
1)Why you must destry the parameter?
2)When you run the code the program doesn't crash when you delete the pointer?

Answer me maybe I can help you

Regards,
Ovidiu

Brendan Cullen
July 29th, 1999, 03:33 AM
Hi Ovidiu,

1 : I do not need to destory the object. But I do need a private data member of my class (vehicle) to be able to point to the object. I am aware that, unfortunately, a (carelsss) programmer can :

delete ptrperson;



2 : Yes - the program crashes. But I'd prefer if the program could not compile.

Regards,

Brendan

Burlacu Ovidiu
July 29th, 1999, 03:45 AM
Why don't declare a member variable like this:

Person& refperson;



?

Tomaz Stih
July 29th, 1999, 03:56 AM
Ave,

the code you posted doesn't make sense (perhaps a typo?) A decent operating system should raise exception when you do *ptrperson=refperson; and that is calling the copy constructor on *ptrperson while ptrperson points to unknown.

Note that if ptrperson was initialized you would not actually delete your reference but instead you would do a shallow copy of the refperson object.

So if your main body would be something like

person me;
vehicle car;
car.ptrperson=new person; // now we have space!
car.destroy(me);



it would work.

On the other side if you want compiler protection and that:

void vehicle::destroy(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 !
}



was what you originally intended to do the compiler will protect your const by dropping an error (unable to cast from const type to type)

Regards,
Tomaz

---------------------------------------------
Tomaz Stih, B.Sc.CS tomaz@nameco.com
Ob sotoccju 10 Nameco Group
SI-1000 Ljubljana http://www.nameco.com
Europe

Brendan Cullen
July 29th, 1999, 04:00 AM
(I used 'arefperson' instead of 'refperson' to distinguish the data member from the parameter name.)

If I do as you suggest :

class vehicle
{
public :
vehicle(void);
person& arefperson;
person* ptrperson;
void destroy(const person & refperson);
};

vehicle::vehicle(void)
{
}



then I receive a compiler error (VC++ 5.0) : "error C2758: 'arefperson' : must be initialized in constructor base/member initializer list".

MS Help says : "If a const or reference member variable is not given a value when it is initialized, it must be given a value in the object constructor."

I'd be okay if it was the constructor of vehicle that receives the object by reference. But it isn't. It is destroy().

Regards,

Brendan

Brendan Cullen
July 29th, 1999, 04:21 AM
Hi Tomaz,

Yes - the code doesn't make sense. It includes the use of an uninitialised pointer. I understand that. But yes - what I was really looking for was compiler protection. So your idea of
ptrperson = &refperson;


instead of

ptrperson = &refperson;


is very good. Indeed, I can go further and make ptrperson a pointer to a const object.
class vehicle
{
public :
const person* ptrperson;
void destroy(const person & refperson);
};


This still provides compiler protection.

Thanks,

Brendan

Tomaz Stih
July 29th, 1999, 04:30 AM
Akhmmm,

well,

Brendan,

did you perhaps forget to rate my post... ;-)

Tomaz

---------------------------------------------
Tomaz Stih, B.Sc.CS tomaz@nameco.com
Ob sotoccju 10 Nameco Group
SI-1000 Ljubljana http://www.nameco.com
Europe