Newsgroup: comp.lang.c++
Subject: The C++ Language 4th edition - Subclassing vector for range checking.
From: mrileyoscmp@...
Date: Sat, 13 Jul 2013 18:26:45 -0700 (PDT)
I was under the impression that it was generally a bad idea to subclass vector because the destructor is not virtual because if someone referenced an object of your class with a pointer and then deleted it with the base, the operation is undefined.
In The C++ Programing Language 4.4.1.2 Stroustrup says "... I often use a simple range-checking adaption of vector:"
template<typename T>
class Vec : public std::vector<T> {
public:
using vector<T>::vector;
T & operator[](int i)
{return vector<T>::at(i);}
const T & operator[](int i) const
{return vector<T>::at(i);}
};
I thought this was some what dangerous because if an user of the class (Maybe not the person who wrote it) writes:
Vector<T> * vectorObject = new Vec<int>(100);
delete vectorObject;
Results in undefined behavior.
Did something change in C++11? Is there some common idiom where people create classes that must be on the stack or they should know not to delete the base?
I ask because this seems like it would create error prone code.
Subject: The C++ Language 4th edition - Subclassing vector for range checking.
From: mrileyoscmp@...
Date: Sat, 13 Jul 2013 18:26:45 -0700 (PDT)
I was under the impression that it was generally a bad idea to subclass vector because the destructor is not virtual because if someone referenced an object of your class with a pointer and then deleted it with the base, the operation is undefined.
In The C++ Programing Language 4.4.1.2 Stroustrup says "... I often use a simple range-checking adaption of vector:"
template<typename T>
class Vec : public std::vector<T> {
public:
using vector<T>::vector;
T & operator[](int i)
{return vector<T>::at(i);}
const T & operator[](int i) const
{return vector<T>::at(i);}
};
I thought this was some what dangerous because if an user of the class (Maybe not the person who wrote it) writes:
Vector<T> * vectorObject = new Vec<int>(100);
delete vectorObject;
Results in undefined behavior.
Did something change in C++11? Is there some common idiom where people create classes that must be on the stack or they should know not to delete the base?
I ask because this seems like it would create error prone code.
via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?41835-The-C-Language-4th-edition-Subclassing-vector-for-range-checking&goto=newpost
View all the progranning help forums at:
http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums
No comments:
Post a Comment