CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    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);
    }





  2. #2
    Join Date
    Jul 1999
    Location
    Romania - Iasi
    Posts
    558

    Re: How to protect object passed by const reference from destruction ?

    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




  3. #3
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: How to protect object passed by const reference from destruction ?

    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


  4. #4
    Join Date
    Jul 1999
    Location
    Romania - Iasi
    Posts
    558

    Re: How to protect object passed by const reference from destruction ?

    Why don't declare a member variable like this:

    Person& refperson;



    ?




  5. #5
    Join Date
    May 1999
    Location
    Slovenia (currently: Germany)
    Posts
    249

    Re: How to protect object passed by const reference from destruction ?

    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: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 !
    }



    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 [email protected]
    Ob sotoccju 10 Nameco Group
    SI-1000 Ljubljana http://www.nameco.com
    Europe



  6. #6
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: How to protect object passed by const reference from destruction ?

    (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


  7. #7
    Join Date
    Apr 1999
    Location
    Dublin, Ireland
    Posts
    64

    Re: How to protect object passed by const reference from destruction ?

    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



  8. #8
    Join Date
    May 1999
    Location
    Slovenia (currently: Germany)
    Posts
    249

    Re: How to protect object passed by const reference from destruction ?

    Akhmmm,

    well,

    Brendan,

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

    Tomaz

    ---------------------------------------------
    Tomaz Stih, B.Sc.CS [email protected]
    Ob sotoccju 10 Nameco Group
    SI-1000 Ljubljana http://www.nameco.com
    Europe



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured