[help] const_cast

Discussion in 'C++' started by Kaspar Minosiants, Jul 21, 2003.

  1. Hi ,
    there is a question about work of const_cast

    The question is why the object is still reachable after a delete operation

    class A {
    int var;
    public:
    A():var(0){}
    ~A(){}
    void setVar(int a){var=a;}
    int getVar() const {return var;}
    int getVar() {return var;}
    };
    int main() {

    const A* a=new A();
    A *b=const_cast<A*>(a);
    delete a;
    b->setVar(10);
    cout<<b->getVar();
    return 0;
    }
    Kaspar Minosiants, Jul 21, 2003
    #1
    1. Advertising

  2. > there is a question about work of const_cast
    >
    > The question is why the object is still reachable after a delete

    operation
    >
    > class A {
    > int var;
    > public:
    > A():var(0){}
    > ~A(){}
    > void setVar(int a){var=a;}
    > int getVar() const {return var;}
    > int getVar() {return var;}
    > };
    > int main() {
    >
    > const A* a=new A();
    > A *b=const_cast<A*>(a);
    > delete a;
    > b->setVar(10);
    > cout<<b->getVar();
    > return 0;
    > }


    Undefined behaviour, anything might happen. const_cast has nothing to do
    with this.
    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Jul 21, 2003
    #2
    1. Advertising

  3. "Kaspar Minosiants" <> wrote in message
    news:bfgiol$eeegs$-berlin.de...
    >
    > Hi ,
    > there is a question about work of const_cast
    >
    > The question is why the object is still reachable after a delete operation
    >
    > class A {
    > int var;
    > public:
    > A():var(0){}
    > ~A(){}
    > void setVar(int a){var=a;}
    > int getVar() const {return var;}
    > int getVar() {return var;}
    > };
    > int main() {
    >
    > const A* a=new A();
    > A *b=const_cast<A*>(a);
    > delete a;
    > b->setVar(10);
    > cout<<b->getVar();
    > return 0;
    > }
    >


    Well what would you expect to happen? Its not a compile error, so what sort
    of error?

    The truth is that accessing an object after it has been deleted is an
    example of 'undefined behaviour', which means ANYTHING could happen,
    including your program 'working'.

    This has nothing to do with const_cast.

    john
    John Harrison, Jul 21, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. drowned

    const_cast question

    drowned, Aug 4, 2003, in forum: C++
    Replies:
    3
    Views:
    484
    Josephine Schafer
    Aug 4, 2003
  2. R. Anbeeswaran

    const_cast<>

    R. Anbeeswaran, Nov 13, 2003, in forum: C++
    Replies:
    7
    Views:
    605
    Ekkehard Morgenstern
    Nov 14, 2003
  3. S.Senthilvel

    const_cast

    S.Senthilvel, Jan 6, 2004, in forum: C++
    Replies:
    4
    Views:
    2,035
    Andrey Tarasevich
    Jan 8, 2004
  4. Gajanan Bhat

    const_cast question

    Gajanan Bhat, May 4, 2004, in forum: C++
    Replies:
    9
    Views:
    550
    Bill Seurer
    May 5, 2004
  5. Tobias Kilian
    Replies:
    2
    Views:
    1,508
    Michiel Salters
    May 25, 2004
Loading...

Share This Page