Could anyone point me in the directions of a good tutorial on these?
thanks
Printable View
Could anyone point me in the directions of a good tutorial on these?
thanks
I learned about them just by searching through posts here and asking questions.
well then for starters, how do you declare a vector of custom objects that accept a value for their constructor? like:
vector<myClass> myObjects(10)--this gives you ten my objects, but where do you put the value for the contructor? like:
myClass myObject("blah")--"blah" being the constructor.
thanks
Howdy,
I just finished learning about vectors and found them to be a pretty useful tool and fairly easy to learn, and I am far from a C++ guru.
Here is a post I just went through trying to figure them help and getting help. CornedBee helped our a ton with the code, then I read the vector help in c++ and MSDN to help me figure out what the code is doing.
Basically, you dont have to declare a vector of a certainn size, which is the cool part of the vector. You just declare the vector, then add your information, in your cass objects, using the push_back() method. The vector will auto resize and then you use the vector::iterator to cycle through the vector to each item. I used strings and structs in my code and the link below demestrates both real well.
Hope that helps
Jerel
http://www.vbforums.com/showthread.p...hreadid=223521
The vector class automatically uses the standard constructor. There's no way to use any special constructor when you specify an initial size.
Every class that is to be contained in any STL container must have a standard constructor, a copy constructor and an assignment operator, but the latter two are generated implicitly and you only need to specify them if you want special functionality.
The standard constructor is also generated implicitly if there is no other constructor available.
BTW the requirement of a standard constructor is not a shortcoming of STL containers. I know of no container class in any library that allows other constructors, and even normal arrays don't.
so basically what your saying is that, if i want to instatiate a class whose contructor requires a value, i can instatiate it in an array or vector?
Assuming that's a typo and you wanted to say "can't", yes.
But that's usually not a big problem as classes that are held by containers are usually some value types (like a class representing a point), other classes are more likely to be contained by pointer. And it shouldn't be a big problem changing the value of a value type.
If you really need constructors then declare an array/vector of pointers and construct each object on the heap. But YOU need to take care that they really get deleted:
Code:template<typename Cont>
inline void WipePtrCont(Cont &cont) {
for(Cont::iterator it=cont.begin();it != cont.end(); ++it)
delete *it;
cont.clear();
}
...can you explain that a little better. i just read the entire chapter on pointers in a c++ book, and i still dont understand anything you just said :[
Reason i need to know is because i want to make an array of heros/enemies for my text RPG, and they are of the type 'charStats' which is a class i wrote myself. and as you discussed earlier, you cant have an array of objects declared with constructor parameters.. which is kinda what i need to do to initialize their stats ;[
vector<charStats> hero("Player1",1,8,9,2);
the parameters being for name, hp, strength, and the like. but where does the array size go.. ? That's what this thread is about, i guess. but i still dont understand how to work around it.
But as I said, don't forget to delete them when you're done.Code:vector<charStats*> vec_pUnit(initSize);
for(int i=0;i<initSize;++i)
{
vec_pUnit[i] = new charStats("Name", 1,8,9,2);
}
ah, that works. thanks =]
one more question about pointers.. if you have a function that takes a pointer as a parameter, then the pointer in the function is still pointing to the same memory address, right? in other words, is it neccessary to delete the pointer twice- once in the main function and again in the called function
It's pointing to the same address. You must delete it once and exactly once. No more, no less.