 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Greg Guest
|
Posted: Sun Dec 24, 2006 5:09 pm Post subject: how do i set the size / reserve of a vector.. |
|
|
How do i set the size / reserve of a vector ?
template < typename T >
class BaseVec : public Vec< T > {
public:
void setsize(const int &sz) { reserve(sz); };
BaseVec(): Vec<T>() { }
};
#define MAX_MODS 128
class ttsmod
{
public :
std::string nm;
int cnt;
};
typedef int modidx;
class ttsmods : public BaseVec<ttsmod>
{
public :
ttsmods();
};
The setsize function does not compile.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Mon Dec 25, 2006 1:20 am Post subject: Re: how do i set the size / reserve of a vector.. |
|
|
"Greg" <imutate (AT) hotmail (DOT) co.uk> writes:
| Quote: | How do i set the size / reserve of a vector ?
|
For a std::vector object, use resize() / reserve().
| Quote: | template < typename T
class BaseVec : public Vec< T > {
|
And what does the name Vec name?
| Quote: | public:
void setsize(const int &sz) { reserve(sz); };
|
Written like this, the name reserve is non-dependant. Among other
things, this means that inherited functions are not considered when it
is looked up.
If that's the problem, one solution is to make the name dependant,
e.g.:
void setsize(int size)
{
this->reserve(size);
}
Note that I have also changed the name and type of the parameter. It
usually doesn't make a lot of sense to pass ints by reference to
const.
Another solution is to remove the member function setsize() and have
the caller call reserve() instead.
BTW: If Vec is another name for std::vector, you have two other
problems:
1) The names setsize and size are badly chosen, since the notion
"size" refers to the number of elements currently in a vector, while
reserve() affects a vector's capacity, i.e. the number of elements the
vector can hold without internal memory reallocation.
2) std::vector and the other Standard Library container templates
weren't designed to serve as public base classes.
| Quote: | BaseVec(): Vec<T>() { }
};
#define MAX_MODS 128
|
Consider replacing macros like this one with enumerators, which are
much better behaved in most situations; e.g.:
enum
{
MAX_MODS = 128
};
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Mon Dec 25, 2006 1:21 am Post subject: Re: how do i set the size / reserve of a vector.. |
|
|
In article <1166930702.230319.237690 (AT) 42g2000cwt (DOT) googlegroups.com>, Greg
<imutate (AT) hotmail (DOT) co.uk> writes
| Quote: | How do i set the size / reserve of a vector ?
Here is the code you left out: |
#include <vector>
#include <string>
#define Vec std::vector
and that last line is awful as it does nothing useful but obfuscates
your code
| Quote: | template < typename T
class BaseVec : public Vec< T > {
std::vector is not designed for derivation, andf in addition your |
derived class name is pure obfuscation.
| Quote: | public:
void setsize(const int &sz) { reserve(sz); };
BaseVec(): Vec<T>() { }
};
#define MAX_MODS 128
class ttsmod
{
public :
std::string nm;
int cnt;
};
typedef int modidx;
class ttsmods : public BaseVec<ttsmod
{
public :
ttsmods();
};
The setsize function does not compile.
|
And it compiles fine with all the compilers I have tried once the lines
you forgot to tell us about here are added (he told us in
alt.comp.lanf.learn.c-c++)
--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Mon Dec 25, 2006 1:21 am Post subject: Re: how do i set the size / reserve of a vector.. |
|
|
On Dec 24, 6:09 am, "Greg" <imut...@hotmail.co.uk> wrote:
| Quote: | How do i set the size / reserve of a vector ?
template < typename T
class BaseVec : public Vec< T > {
public:
void setsize(const int &sz) { reserve(sz); };
BaseVec(): Vec<T>() { }
};#define MAX_MODS 128
I'm not sure just what "Vec" is , however if you intended std::vector, |
then it should look like this:
template < typename T >
class BaseVec : public std::vector< T > {
public:
void setsize(std::size_t sz) //type that reserve really wants--
change quiet scompiler warnings
{
reserve(sz);
};
// compiler generated default constructor fine in this case
};
However, note how the result behaves:
void foo(){
BaseVec<int> myvec ;
assert(myvec.size()==0); //size() from std::vector
myvec.setsize(20);
assert(myvec.size()==0);
assert(myvec.capacity()>=20); //capacity() from std::vector
myvec.resize(40); //resize() from std::vector
assert(myvec.resize()==40);
assert(myvec.capacity()>=40); //capacity() from std::vector
std::vector<int> * myvec2 =new BaseVec<int>;
delete myvec2; //illegal C++ code, std:: vector does not
have a virtual destructor
}
"std::vector::reserve(x)" only makes sure that the guts of your vector
is capable of holding x instances of T-- it doesn't actually create
them. std::vector::capacity() will tell you just how many copies of T
your vector will hold without having to reallocate its internals, and
copy all the live instances to the new storage. Recall that std::vector
has the property that the guts are always contiguous. Take note that
std::vector is the only std container that has these functions -- using
them locks you in.
However, size() refer to how many instances of T are actually in the
container. resize() will construct instances of T either by default
constructing them, or by copy constructor using resize(std::size_t , T
const&); The "sequence" containers have these (namely vector list, and
deque) and the corresponding container adapters "stack" and "queue"
On the public inheritance, if your program is small enough or tools
good enough to easily detect the illegal deletion through base class
pointer, then the public inheritance isnt a problem --in fact it solves
more problems than it creates, which is what you want. I use this
design myself, the moral is just be aware of the caveats.
Hope this helps
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Mon Dec 25, 2006 1:22 am Post subject: Re: how do i set the size / reserve of a vector.. |
|
|
Greg wrote:
| Quote: | How do i set the size / reserve of a vector ?
|
| Quote: | class BaseVec : public Vec< T > {
|
What is Vec?
| Quote: | public:
void setsize(const int &sz) { reserve(sz); };
|
Passing an int by const reference is unjustified additional complexity.
| Quote: |
The setsize function does not compile.
|
So do you want to give us a clue what error you got? I suspect
there's no function "reserve" in whatever goofy class Vec is.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ivan Novick Guest
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|