How can I use typedef and auto to clean up my unique_ptr<vector> declaration throughout my projects classes?

Hello, I'm new to c++ so any help will be appreciated.

When I'm making a weapons declaration it's quite cumbersome and long. It would be nice to be able to type weapons_t from anywhere in my code.

1
2
3
4
#include <vector>
#include <memory>

vector<unique_ptr<CWeapons>> weapons;


I was wondering how I could utilise typedef and auto to make a weapons_t type definition. And how I could use it in multiple classes. My weapons declaration is all over my code and in many classes. Due to the polymorphism aspects of the program. Thank you.

Thank you.
Last edited on
Maybe like this:
1
2
3
4
5
6
7
using weapons_t = std::vector<std::unique_ptr<CWeapons>>;

int main()
{
    weapons_t weapons;
    
}
And would I have to copy that into every file?
Last edited on
You could put the using statement into a header file.
The header file that defines CWeapons would be the obvious place to put it, because everywhere that wants to use weapons_t is going need to have CWeapons defined anyway.
Last edited on
Thanks, guys. I'll close this now. I realised that I had to place it below the class itself.

1
2
3
4
5
6
class CWeapons
{

}

typedef std::vector<std::unique_ptr<CWeapons>> weapons_t;
Last edited on
Topic archived. No new replies allowed.