Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Object Destruction with / without Virtual Function

Reply
Thread Tools

Object Destruction with / without Virtual Function

 
 
V Patel
Guest
Posts: n/a
 
      01-30-2007
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



 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      01-30-2007
"V Patel" <(E-Mail Removed)> wrote in
news:kYvvh.50$(E-Mail Removed):

> 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.

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-30-2007
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.

 
Reply With Quote
 
Ambivali
Guest
Posts: n/a
 
      01-30-2007

"V Patel" <(E-Mail Removed)> wrote in message
news:kYvvh.50$(E-Mail Removed)...
>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;
}



 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      01-30-2007
"Ambivali" <(E-Mail Removed)> wrote in
news:0iwvh.51$(E-Mail Removed):

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

 
Reply With Quote
 
Yahooooooooo
Guest
Posts: n/a
 
      01-30-2007
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" <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual inheritance destruction issue oliver C++ 2 07-14-2011 04:22 PM
virtual base class and construction/destruction order BeautifulMind C++ 7 02-08-2007 12:35 PM
virtual function and pure virtual function in the implementation of COM IK C++ 2 07-23-2004 02:55 PM
Re: virtual destruction when? Andrey Tarasevich C++ 4 08-26-2003 05:13 PM
Re: virtual destruction when? Andrew Koenig C++ 1 08-23-2003 07:09 AM



Advertisments