Jump to content
  • Advertisement
Sign in to follow this  
floatingwoods

Deriving from std::vector

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Well your pursuit sounds dubious.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!