Non-virtual destructors & valarray memory allocation

  • Thread starter Michael Hopkins
  • Start date
M

Michael Hopkins

Hi all

A couple of questions that are influencing the design of some classes.


1) Why were the std:: library containers not given virtual destructors - is
this a feature or an oversight? If the latter, I wonder if it would be
something that could be changed easily in the upcoming standard without
breaking current code.

We would like to derive publicly from them in a couple of cases and now have
to be more disciplined than we should need to be about how we use those
types.


2) Is there any way of declaring e.g. two valarray<double> so that they are
guaranteed to be contiguous in memory? i.e.

class V {
std::valarray<double> v1, v2;
}


TIA and please CC replies to this address

Michael


_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

_/ _/ _/_/_/ Hopkins Research Ltd
_/ _/ _/ _/
_/_/_/_/ _/_/_/ http://www.hopkins-research.com/
_/ _/ _/ _/
_/ _/ _/ _/ 'touch the future'

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 
K

Kai-Uwe Bux

Michael said:
Hi all

A couple of questions that are influencing the design of some classes.


1) Why were the std:: library containers not given virtual destructors -
is
this a feature or an oversight? If the latter, I wonder if it would be
something that could be changed easily in the upcoming standard without
breaking current code.

We would like to derive publicly from them in a couple of cases and now
have to be more disciplined than we should need to be about how we use
those types.

There is an easy fix:

template < typename T >
class inheritable_vector : public std::vector<T> {

inheritable_vector ( void )
: std::vector<T>()
{}

template < typename A >
inheritable_vector ( A a )
: std::vector<T>( a )
{}

// ... more constructors

virtual
~inheritable_vector ( void ) {}

};

Now, inheritable_vector<T> has a virtual destructor, it has the same
interface as std::vector<T>, and you can use pointers to
inheritable_vector<T> polymorphically. Just never use std::vector (which is
easy to ensure using grep and easy to patch using awk) and you are fine.

2) Is there any way of declaring e.g. two valarray<double> so that they
are guaranteed to be contiguous in memory? i.e.

class V {
std::valarray<double> v1, v2;
}

I don't think so. Maybe, there are platform specific ways.



Best

Kai-Uwe Bux
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top