Object Destruction with / without Virtual Function

V

V Patel

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
 
A

Andre Kostur

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

Rolf Magnus

V said:
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.
 
A

Ambivali

V Patel said:
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;
}
 
A

Andre Kostur

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

Yahooooooooo

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top