473,946 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object Destruction with / without Virtual Function

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

Jan 30 '07 #1
5 1289
"V Patel" <vz******@veriz on.netwrote in
news:kY******** *****@newsfe09. lga:
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.

Jan 30 '07 #2
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.

Jan 30 '07 #3

"V Patel" <vz******@veriz on.netwrote in message
news:kY******** *****@newsfe09. lga...
>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;
}

Jan 30 '07 #4
"Ambivali" <am******@opton line.netwrote in
news:0i******** *****@newsfe09. lga:
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;
}
Jan 30 '07 #5
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" <vzevi...@veriz on.netwrote:
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

Jan 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2136
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I can get! Consider 3 classes in the following heirarchy: base / \ deriv1 deriv2 \
6
7984
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a Child object goes out of scope? Specifically, I am dealing with a C library that provides a function that must be called to "destruct" a particular struct (this struct is dynamically allocated by another provided function). To avoid memory
8
3535
by: al.cpwn | last post by:
Which sections of the 2003 standard should I study to understand the object model? I am looking to find information similar to that in Stanley B. Lippman's book, "Inside the C++ Object Model"
5
3362
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
7
3419
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case class is derived as virtual? How does the order of construction/destruction is impacted if the base class is derived as virtual or non virtual? just see the example below.
2
1439
by: Bastien Durel | last post by:
Hello, I have a problem with some code using exceptions: I throw an exception in a method, catch it in the caller, but the instance seems to be destroyed before the handler can use it ... here is the caller code : try { LOG("Init parser");
1
1415
by: W Karas | last post by:
Virtual functions could have multiple definitions, for different object states. The prototype of a virtual member function could have an optional reserve word "for" followed by a comma-separated list of state names. The statement: virtual using <state>;
275
12655
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
23
3174
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object model: that most OO features are not available for class object design without loss of POD-ness. So, I'm more than leaning toward "bad" because of the limitations and
0
10149
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11549
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8240
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7402
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6317
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.