0

I'm fairly new to c++ and this problem's got me bad. In my project I'm using SFML libraries to create asteroids. I declared a class named _asteroids and want to create a bunch of asteroid objects inside the class. Someone suggested I stored the objects inside an array. Later I learned that it wasn't possible to store objects inside an array, but you could store pointers to objects.

In summary I want to:

-Create objects inside the _asteroids class

-Store the pointers to those objects inside an array

-Be able to call those objects by their pointers from the array and change their values

How do I go about doing that? Here is some of my code to do with this:

class _asteroids{
    float angle; //Angle
    sf::Vector2f a_pos; //Position
    sf::Vector2f a_vel; //Velocity
    void makeAsteroids(int); //This generates the asteroid's appearance
    bool Remove(); //If this is false the object is removed

    public:
    void Update(); //This updates the position
};
8
  • 1
    To answer the question in the title, just like a normal array? What have you tried? What problems do you have with your attempt? Also, if you want to add and remove objects from the "array" at run-rime then consider std::vector instead. Commented Dec 19, 2015 at 13:22
  • 1
    Which part is the issue: creating objects, getting pointers to them, or putting things in an array? Commented Dec 19, 2015 at 13:24
  • 1
    I've tried to use arrays like so: foo[] = new _asteroids Commented Dec 19, 2015 at 13:24
  • 1
    The declaration int foo[] declares an empty array of int. C-style arrays needs to have a fixed size at time of compilation, and of course be an array of the objects you want in the array, e.g. _asteroids foo[64]. Commented Dec 19, 2015 at 13:24
  • 1
    @user2840146 It does not make sense having asteroids, but not asteroid. Commented Dec 19, 2015 at 13:25

2 Answers 2

1

In C++ you should - if at all possible/reasonable - avoid (raw) pointers and C-style arrays. C++ provides standard containers. Like C-style arrays they can hold all kind of element types but they have some benefits that a C-style array doesn't. For instance most containers can grow/shrink when you need it - in other words, their size can adjust itself as you add or remove elements. On top of that, they have a number of member function that will allow you to operate on the contained elements in an easy way.

The vector container is very popular. In many aspects it is just like an array once the elements have been added, i.e. you access elements using var[i]. You can add elements to the end of the vector using push_back.

Example:

std::vector<_asteroids> allAsteroids;

allAsteroids.push_back(_asteroids()); // Add _asteroids object to end of vector
allAsteroids.push_back(_asteroids()); // Add _asteroids object to end of vector
allAsteroids.push_back(_asteroids()); // Add _asteroids object to end of vector

cout << allAsteroids.size() << endl;  // Will print 3

for (auto& a : allAsteroids)  // Iterate over all asteroids in the vector
{
    a.Update();  // Call Update for current element
}

// The same as above in a different way
for (int i = 0; i < allAsteroids.size(); i++)
{
    allAsteroids[i].Update();  // Call Update for i'th element
}

This was just a few examples of what you can do with vector. There are many more.

Check http://www.cplusplus.com/reference/vector/vector/ for more information - all member function of vector is shown to the left on that site.

Sign up to request clarification or add additional context in comments.

Comments

1

Sure you can save Asteroid within an std::vector

class Asteroid {};

std::vector<Asteroid> asteroids;

Asteroid as1;
asteroids.push_back(as1);

2 Comments

what does the asteroids.push_back do?
Thanks, it worked nicely: placing all the class objects inside the vector allows me to destroy and keep track of them just the way I wanted to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.