# Deriving from std::vector

This topic is 3624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'd like to derive template std::vector so that I get an int-value instead of a size_type-value when calling the .size() member function. What I have looks like following and it is declared in the stdafx.h. The problem is that I now get error messages as follows: error C2661: 'CMyVector<T>::CMyVector' : no overloaded function takes 2 arguments or error C2664: 'CMyVector<T>::CMyVector(const CMyVector<T> &)' : cannot convert parameter 1 from 'int' to 'const CMyVector<T> &' What is wong?
template <class T> class CMyVector :public std::vector<T>
{
public:
int size() const
{
return (int(std::vector<T>::size()));
}
};
#define stdVector CMyVector



##### Share on other sites
size_type is an int (it's just going to be unsigned). This is a horrible reason to derive from vector -- what do you feel you need this for?

##### Share on other sites

First of all containers are not designed to be inherited. The biggest reason why is the fact they do not have virtual destructors.

Additionally I have no idea why size_type is not sufficient for you? What difference would it make to you what type it is as long as its integral (which it is)?

##### Share on other sites
This would be the absolutely worst possible reason to do something like that.

Show the calling code that causes the original problem. It's an indication of something being very wrong there - not in the vector.

##### Share on other sites
I agree with the rest of the posters that this is a horrible reason to derive from std::vector. However, the fact that std::vector.size returns an unsigned integral type often does cause the annoying warning "warning: comparison between signed and unsigned integer expressions". Also, explicitly casting all over the place to get rid of this is really annoying. I try to use size_t wherever I don't absolutely need signedness to minimize this annoyance.

##### Share on other sites
Well, I just wanted to quickly try to get rid of all my warnings linked to following type of expression (however I hardly know what a template is.. ):

for (int i=0;i<aVector.size();i++)
...

Which gives me following warning:

warning C4018: '<' : signed/unsigned mismatch

I could disable this type of warning, but I then warnings for more useful cases would also be disabled... or I could replace about 1000 occurences of above-type loop with:

for (int i=0;i<int(aVector.size());i++)
...

or

for (unsigned int i=0;i<aVector.size();i++)
...

(the latter case would require me to look more carefully at the code I change)

##### Share on other sites
Just do int size(std::vector const& v) { return static_cast<int>(v.size()); } ?

And yeah, I think you're doing something wrong elsewhere.

EDIT: Okay, seeing your code -- what exactly do you think is wrong with the last loop? If you loop in the interval <0; v.size()), you obviously don't need a signed integer -- its value never will be negative, no matter if you use unsigned or signed int. What exactly do you think you would have to be careful about?

##### Share on other sites
Edit: I should read posts more carefully. Making the loop counter unsigned is more correct, but TBH I do a mix of both.

##### Share on other sites
The latter case (making the loop iteration type unsigned) is the correct solution.

##### Share on other sites
Thanks for all your input, I know I am a horrible c/c++ programmer ;)

• 38
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631362
• Total Posts
2999577
×