Object Destruction with / without Virtual Function

Discussion in 'C++' started by V Patel, Jan 30, 2007.

  1. V Patel

    V Patel Guest

    I am trying to understand the behaviour for the following two cases:

    class A {
    virtual funcA() {i++;}; // case 1
    funct A() {i++;}; // case 2
    private:
    int i;
    };

    main ()
    {
    A *a = new A();
    A *b = a;
    delete a;
    b->functA()
    }

    case 1 fails (is this because it has to go thru vptr table and the object is
    gone)
    case 2 ok. (is this because its static binding. Though no object. Isn't this
    unsafe?)

    Also, are default constructor, copy constructor etc required from mere
    presence
    of any virtual function (non-trivial case) in a class?

    thanks
     
    V Patel, Jan 30, 2007
    #1
    1. Advertising

  2. V Patel

    Andre Kostur Guest

    "V Patel" <> wrote in
    news:kYvvh.50$:

    > I am trying to understand the behaviour for the following two cases:
    >
    > class A {
    > virtual funcA() {i++;}; // case 1
    > funct A() {i++;}; // case 2
    > private:
    > int i;
    > };
    >
    > main ()
    > {
    > A *a = new A();
    > A *b = a;
    > delete a;
    > b->functA()


    Undefined Behaviour. Dereferencing a pointer to deleted memory. Anything
    could happen.

    > }
    >
    > case 1 fails (is this because it has to go thru vptr table and the
    > object is gone)
    > case 2 ok. (is this because its static binding. Though no object.
    > Isn't this unsafe?)


    Also, a couple of nitpicks:

    1) funcA() has no return type. Invalid.
    2) case 2: What type is "funct" ? And this function has no return
    statement... Undefined Behaviour (falling off the end of a function that
    has a return type w/o a return statement).
    3) case 2: this looks like a constructor, but has a return value, also not
    allowed.
    4) main doesn't have a return type. Also invalid. You must declare it
    with an 'int' return type (to be Standard compilant).

    > Also, are default constructor, copy constructor etc required from mere
    > presence
    > of any virtual function (non-trivial case) in a class?


    Nope. However, a virtual destructor is highly recommended as deletion of a
    subclass via a base class pointer, and the destructor _isn't_ virtual leads
    to Undefined Behaviour too.
     
    Andre Kostur, Jan 30, 2007
    #2
    1. Advertising

  3. V Patel

    Rolf Magnus Guest

    V Patel wrote:

    > I am trying to understand the behaviour for the following two cases:


    You should always post real code that you actually tried. The code below
    contains lots of errors.

    > class A {
    > virtual funcA() {i++;}; // case 1
    > funct A() {i++;}; // case 2
    > private:
    > int i;
    > };
    >
    > main ()
    > {
    > A *a = new A();
    > A *b = a;
    > delete a;
    > b->functA()
    > }
    >
    > case 1 fails (is this because it has to go thru vptr table and the object
    > is gone)
    > case 2 ok. (is this because its static binding. Though no object. Isn't
    > this unsafe?)


    Both are formally undefined behavior, so anything can happen.

    > Also, are default constructor, copy constructor etc required from mere
    > presence of any virtual function (non-trivial case) in a class?


    Not sure what you mean here. The constructor you spend your class have
    nothing to do with whether the class has virtual member functions.
     
    Rolf Magnus, Jan 30, 2007
    #3
  4. V Patel

    Ambivali Guest

    "V Patel" <> wrote in message
    news:kYvvh.50$...
    >I am trying to understand the behaviour for the following two cases:
    >
    > class A {
    > virtual funcA() {i++;}; // case 1
    > funct A() {i++;}; // case 2
    > private:
    > int i;
    > };
    >
    > main ()
    > {
    > A *a = new A();
    > A *b = a;
    > delete a;
    > b->functA()
    > }
    >
    > case 1 fails (is this because it has to go thru vptr table and the object
    > is gone)
    > case 2 ok. (is this because its static binding. Though no object. Isn't
    > this unsafe?)
    >
    > Also, are default constructor, copy constructor etc required from mere
    > presence
    > of any virtual function (non-trivial case) in a class?
    >
    > thanks
    >
    >
    >


    Sorry for a bit sloppy in the original example. The class does not have any
    constructor.
    For both test-cases, the functA() returns void.

    class A {
    virtual void funcA() {i++;}; // case 1
    void functA() {i++;}; // case 2
    // no constructor or destructor.
    private:
    int i;
    };

    int main ()
    {
    A *a = new A();
    A *b = a;
    delete a;

    b->functA();
    return 0;
    }
     
    Ambivali, Jan 30, 2007
    #4
  5. V Patel

    Andre Kostur Guest

    "Ambivali" <> wrote in
    news:0iwvh.51$:

    > For both test-cases, the functA() returns void.
    >
    > class A {
    > virtual void funcA() {i++;}; // case 1
    > void functA() {i++;}; // case 2
    > // no constructor or destructor.
    > private:
    > int i;
    > };
    >
    > int main ()
    > {
    > A *a = new A();
    > A *b = a;
    > delete a;
    >
    > b->functA();


    Same Undefined Behaviour. You're dereferencing a pointer which has already
    been deleted. Anything can happen. Doesn't matter whether the function is
    virtual or not.

    > return 0;
    > }
     
    Andre Kostur, Jan 30, 2007
    #5
  6. V Patel

    Yahooooooooo Guest

    your code still doesn't work all the member function are private..
    Is this what you looking for...

    1 #include <iostream>
    2 using namespace std;
    3 class A {
    4
    5
    6 public:
    7 A() { i=0; }
    8 //virtual void functA() {i++; cout << i <<
    endl;}; // case 1
    9 void functA() {i++; cout << i <<
    endl;}; // case 2
    10 void f(void);
    11
    12 private:
    13 int i;
    14
    15 };
    16
    17
    18 int main ()
    19 {
    20 A *a = new A();
    21 A *b = a;
    22 delete a;
    23 b->functA();
    24 return 0;
    25 }
    26


    On Jan 30, 5:04 am, "V Patel" <> wrote:
    > I am trying to understand the behaviour for the following two cases:
    >
    > class A {
    > virtual funcA() {i++;}; // case 1
    > funct A() {i++;}; // case 2
    > private:
    > int i;
    >
    > };
    >
    > main ()
    > {
    > A *a = new A();
    > A *b = a;
    > delete a;
    > b->functA()
    >
    > }
    >
    > case 1 fails (is this because it has to go thru vptr table and the object is
    > gone)
    > case 2 ok. (is this because its static binding. Though no object. Isn't this
    > unsafe?)
    >
    > Also, are default constructor, copy constructor etc required from mere
    > presence
    > of any virtual function (non-trivial case) in a class?
    >
    > thanks
     
    Yahooooooooo, Jan 30, 2007
    #6
    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. Andrey Tarasevich

    Re: virtual destruction when?

    Andrey Tarasevich, Aug 22, 2003, in forum: C++
    Replies:
    4
    Views:
    561
    John L Fjellstad
    Aug 26, 2003
  2. Andrew Koenig

    Re: virtual destruction when?

    Andrew Koenig, Aug 22, 2003, in forum: C++
    Replies:
    1
    Views:
    423
    John Harrison
    Aug 23, 2003
  3. IK
    Replies:
    2
    Views:
    639
    hemraj
    Jul 23, 2004
  4. BeautifulMind
    Replies:
    7
    Views:
    690
    Ron Natalie
    Feb 8, 2007
  5. oliver
    Replies:
    2
    Views:
    252
    red floyd
    Jul 14, 2011
Loading...

Share This Page