Vectors & Objects/Classes

Hey guys, I require some assistance with vectors & classes. A bit stuck so could use some guidance.

I have a class called Collectable.
At the moment within that class it is using an array of Coords (each coord has an X & Y). The array is fixed to 1 with a const int, this is so that only one 'Collectable' is ever spawned. The 'Collectable' class knows how to render itself with a gotoxy function which takes in a coord as a parameter & I loop through the array and draw whatever is in it at the coords it is at.
It also has a random position function which sets it position within a certain x & y area.

I wanted to build upon the 'Collectable' class & create another class that would use vectors which I could dynamically create multiple 'Collectable' objects with.

I created a 'collecthandler' class which contains the vector of Collectables in it.
I've done some research and I think I have set up a Vector of Collectables. However I am unsure how to proceed further. Since the original 'Collectable' class has everything to do with a 'Collectable'. I want to be able to use the methods within it.

I believe when I create the 'new collectable' within the vector it calls the 'Collectable' default constructor. I'm not sure whether I need to build upon its default constructor because at the moment all it does is setup a char value & bool for me. The rest is done within methods.

Ill paste some code below if my description was not able to help you understand my problem!

Thanks & sorry for the lengthy post.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  class Collectable
{
private:
Coord m_Collectable       [m_maxCollectable]; //m_maxCollectable set to 1.

void renderCollectable(const Coord &coord); //how I render the collectable.

}

class CollectableHandler
{

private:
vector <Collectable*> m_collectables;

CollectableHandler::CollectableHandler()
{
	m_collectables.push_back(new Collectable());
}

}
Last edited on
Not sure what your question is.

I believe when I create the 'new collectable' within the vector it calls the 'Collectable' default constructor.

Correct.

I'm not sure whether I need to build upon its default constructor because at the moment all it does is setup a char value & bool for me.

That all depends on what you want to do.

Is there a reason your vector is a collection of raw pointers, rather than copies of Colllectables?
Using raw pointers is fraught with opportunities to mess things up by incurring memory leaks, or releasing things at the wrong time. If you really need to use pointers, at least use a std::unique_ptr.
http://www.cplusplus.com/reference/memory/unique_ptr/?kw=unique_ptr



Thanks for the reply.

So basically I would like to create a collectable and add it to my vector dynamically when I want / need to, I think you helped clear up what I actually need. I think I just want a copy of another collectable or multiple copies depending on how many are pushed in to the vector, with varied x & y positions.
I think maybe I am using the pointers incorrectly for this then.

In regards to the default construct would I just need to setup one collectable object and then when it gets called from my collectable handler class from the vector it will also do the same?
would I just need to setup one collectable object and then when it gets called from my collectable handler class from the vector it will also do the same?

I'm not sure I understand what you mean by that.

Line 16: I don't believe you want CollectibleHandler to push a new default Collectible onto the vector. A collectible of what? And what are it's coordinates? What you're saying is that CollectibleHandler always creates a vector of one default Collectible.

CollectibleHandler should have an Add function that adds a Collectible to the vector. Then where ever you determine the initialize population of Collectibles, you would call CollectibleHandler::Add (collectible).

Since collectible has a Coord attribute, I would give Collectible a constructor that accepts a Coord.

It's not clear to me what functionality CollectibleHandler provides besides being a wrapper for std::vector. You may have more plans for CollectibleHandler, but at this point, you can accomplish the same thing with:

 
vector<Collectible>  v_collectibles;

.CollectibleHandler should have an Add function that adds a Collectible to the vector. Then where ever you determine the initialize population of Collectibles, you would call CollectibleHandler::Add (collectible).


Really sorry about the confusion, I know what I want in my head, just not describing it the best way!
Right, I think you explained it much better for me with what I have quoted above from your previous post. I want to be able to add collectables to the vector dynamically. So I can add and remove then whilst running the program.

I didn't quite understand though how I would setup their coordinates but after reading your post this should all be done in the collectible class. The collectablehandler class can just for now have a method that adds a collectable to it when I want if I understand you correctly and the collectable it adds, their properties would be setup in the collectable class. I.e coords etc.


Last edited on
Yup. You got it.

Not sure how well you understand inheritance yet. You could even have CollectibleHandler inherit from std::vector.

1
2
3
4
5
class CollictibleHandler : public vector<Collectible>
{
public:
  // public methods
};


This would allow you to call CollectibleHandler::push_back(Collectible) to add an item to the vector since all of vector's public methods would be available to an instance of CollectibleHandler.

Thanks for the help AbstractionAnon!

I re-did the class and thought about how it should work, I redid my 'collectable' class and changed 'collectablehandler' accordingly. I went with the code below, and I am now able to dynamically add objects!

Thanks once again :)


vector <Collectable*> v_collectables;
Last edited on
Topic archived. No new replies allowed.