473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Why do we need non-virtual destructor?

Why do we need non-virtual destructor?
Because of vtable?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Dec 5 '05 #1
13 6965

Alex Vinokur wrote:
Why do we need non-virtual destructor?
Because of vtable?


What exactly are you asking?

The destructor should be virtual if the class is polymorphic, so that
you can properly delete the derived class's object through pointer to
the base class.

If the class is not polymorphic (and hence not intended to be used as
base class) then destructor should be non-virtual.

Dec 5 '05 #2

"Neelesh Bodas" <ne***********@gmail.com> wrote in message news:11**********************@g47g2000cwa.googlegr oups.com...

Alex Vinokur wrote:
Why do we need non-virtual destructor?
Because of vtable?
What exactly are you asking?


If are there situations that we _must_ use _non-virtual_ destructor?

The destructor should be virtual if the class is polymorphic, so that
you can properly delete the derived class's object through pointer to
the base class.

If the class is not polymorphic (and hence not intended to be used as
base class) then destructor should be non-virtual.


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Dec 7 '05 #3
Alex Vinokur wrote:
"Neelesh Bodas" <ne***********@gmail.com> wrote:

Alex Vinokur wrote:
Why do we need non-virtual destructor?
Because of vtable?


What exactly are you asking?


If are there situations that we _must_ use _non-virtual_ destructor?


Most...

Why would you expect to use Point class as a base?

class Point
{
public:
Point(double x, double y) : _x(x), _y(y) {}
~Point()
//...
private:
double _x;
double _y;
}

There are plenty of non-polimorphic classes, classes used in OOP
constructions (i.e. design patterns) where you don't expect to use
inheritance. So, in such cases you don't use virtual constructors
(virtual methods, in general).

--
Mateusz Loskot
http://mateusz.loskot.net

Dec 7 '05 #4
Mateusz Loskot wrote:

There are plenty of non-polimorphic classes, classes used in OOP
constructions (i.e. design patterns) where you don't expect to use
inheritance.


I mean "where you don't expect to use" as "where, in some cases, you
don't need inheritance".

--
Mateusz Loskot
http://mateusz.loskot.net

Dec 7 '05 #5
Alex Vinokur wrote:
If are there situations that we _must_ use _non-virtual_ destructor?


Well, if, by "must" you mean syntactic correctness, then no, C++
doesnot ever enforce that from the syntactic angle.
But if you are talking about good techniques of writing code ( logical
correctness), then any class which is _not_ supposed to act as a base
class should have a public non-virtual destructor. But note that its
not again a _must_ : rather, it is a coding technique that one should
always follow.

Dec 8 '05 #6

"Mateusz Loskot" <ma*****@loskot.net> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Why would you expect to use Point class as a base?


Many reasons, really! For instance, a mouse cursor position is a Point.

- Risto -
Dec 8 '05 #7
Risto Lankinen wrote:
"Mateusz Loskot" <ma*****@loskot.net> wrote in message:

Why would you expect to use Point class as a base?


Many reasons, really! For instance, a mouse cursor position is a Point.


Hm, I'd do it by means of Composition.

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net

Dec 8 '05 #8
Risto Lankinen wrote:
"Mateusz Loskot" <ma*****@loskot.net> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Why would you expect to use Point class as a base?

public base!
Many reasons, really! For instance, a mouse cursor position is a Point.


Composition would probably make more sense than public inheritance.

Why would you want to access points polymorphically?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Dec 9 '05 #9

"Ben Pope" <be*************@gmail.com> wrote in message
news:11*****************************************@r oc.usenetexchange.com...

Why would you want to access points polymorphically?


* I have a set of functions that manipulate points. Examples:

Distance operator-( const Point &,const Point & );
Distance operator/( const Distance &,int );
Point operator+( const Point &,const Distance & );
Point operator+( const Distance &,const Point & );

* Class Point has a virtual assignment operator.

Then, for example, the following function...

void CenterToRect( Point &pt,const Rectangle &rc )
{
pt = rc.TopLeft() + (rc.BottomRight()-rc.TopLeft())/2;
}

.... could be called as...

CenterToRect( the_cursorpos,a_window );

.... and magically the mouse cursor is centered wrt/ the window.

This cannot be done polymorphically if a Point is but contained
by the class CursorPos.

- Risto -
Dec 12 '05 #10
Risto Lankinen wrote:
"Ben Pope" <be*************@gmail.com> wrote in message:

Why would you want to access points polymorphically?


* I have a set of functions that manipulate points. Examples:

Distance operator-( const Point &,const Point & );
Distance operator/( const Distance &,int );
Point operator+( const Point &,const Distance & );
Point operator+( const Distance &,const Point & );

* Class Point has a virtual assignment operator.

Then, for example, the following function...

void CenterToRect( Point &pt,const Rectangle &rc )
{
pt = rc.TopLeft() + (rc.BottomRight()-rc.TopLeft())/2;
}

... could be called as...

CenterToRect( the_cursorpos,a_window );

... and magically the mouse cursor is centered wrt/ the window.

This cannot be done polymorphically if a Point is but contained
by the class CursorPos.


Yes, it can be done using composition even better:

class Point
{
double _x;
double _y;
public:
Point(double x, double y) : _x(x), _y(y) {}
};

class Cursor
{
Point pt;
public:
Center(const Rectangle& rc)
{
pt = rc.TopLeft() + (rc.BottomRight() - rc.TopLeft()) / 2;
}
};

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net

Dec 12 '05 #11

"Mateusz Loskot" <ma*****@loskot.net> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Yes, it can be done using composition even better:


Your example doesn't do the same thing. In particular, it does not
move the mouse cursor to the designated point.

Let me elaborate:

struct Point
{
virtual void Get( int &,int & ) = 0;
virtual Point &operator=( const Point & ) = 0;
virtual ~Point() { }
};

struct PointXY : Point
{
int x;
int y;
PointXY( int x,int y ) x(x) , y(y) { }
PointXY( const Point &pt ) { pt.Get(x,y); }
void Get( int &a,int &b ) { a = x; b = y }
Point &operator=( const Point &pt ) { return *this = PointXY(pt); }
virtual PointXY &operator=( const PointXY &pt ) { pt.Get( x,y ); }
};

struct CursorPos : Point
{
CursorPos() { }
CursorPos( const Point &pt ) { int x,y; pt.Get(x,y);
::SetCursorPos(x,y); }
void Get( int &a,int &b ) { ::GetCursorPos( a,b ); }
Point &operator=( const Point &pt ) { return *this = CursorPos(pt); }
};
Cheers!

- Risto -
Dec 12 '05 #12
> If are there situations that we _must_ use _non-virtual_ destructor?

Do you want to work with data structures that are value objects?
http://c2.com/cgi/wiki?ValueObject

Regards,
Markus

Dec 12 '05 #13

Alex Vinokur wrote:
Why do we need non-virtual destructor?
Because of vtable?


That's an implementation detail. The ISO standard reason is that any
virtual function destroys "POD-ness". That means that you cannot
memcpy a class if you have a virtual destructor, nor some other exotic
things that you probably don't want to do either.

(Implementations usually compile/run a bit smaller/faster, but this
rarely
matters. )

HTH,
Michiel Salters

Dec 12 '05 #14

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

Similar topics

10
by: Beach Potato | last post by:
Dear Y'all: I'm about to start porting a big old project written in anscient version of Delphi to something more stable, robust, supportable and maybe even portable. Since I haven't seriously...
3
by: Jack A | last post by:
OK Guys. I'm fed up of the query below taking too much time. I CANT change the query since it is generated by a 3rd party product. I can change indexes and add new indexes though. The schema of...
2
by: Oliver Burnett-Hall | last post by:
I'm trying to move to using tableless page layouts, but I've come across what appears to be a bug in IE5's rendering that I can't find a way to overcome. The page has a sidebar to the left of...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
3
by: Drewdog | last post by:
I am getting some error messages which I can't figure out their meaning. I have the code setup, I think it's correct but it doesn't work. My goal is to get this program to read from a data file and...
5
by: Confused User | last post by:
I am working on device that utilizes a Motorola 68HC16 microcontroller. I am using an old unsupported piece of crap Whitesmith's / Intermetrics / Tasking compiler. The embedded compiler business...
70
by: rahul8143 | last post by:
hello, 1) First how following program get executed i mean how output is printed and also why following program gives different output in Turbo C++ compiler and Visual c++ 6 compiler? void main()...
7
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
15
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
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...

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.