Jump to content
  • Advertisement
Sign in to follow this  
xinfinite33

static std::deque inside of template class

This topic is 2130 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

im having a problem with a static deque inside of one of my classes. i know that you have to initialize static members of a class before you can use them but i cant figure out how to initialize a static deque inside of a template class. a little help please?

 

#include <iostream>
#include <deque>

using namespace std;



template <typename T>
class jar
{
    public:
    T * value;
    static int jars;
    static deque<jar*> jarlist;

    template<typename S>
    jar(S obj) //constructor
    {
        this->value= &obj;
        jars++;
        jarlist.push_back(this);
    }

    void destroy() //destroys a jar
    {
        {
            typename deque<jar*>::iterator iter;

            for (iter=jarlist.begin();iter!=jarlist.end();iter++)
            {
                if(*iter==this)
                {
                    delete (*iter);
                    jarlist.erase(iter);
                }
            }
            jars--;
        }
    }

};

template<typename T> int jar<T>::jars=0;
template<typename T> deque<jar*> jar<T>::jarlist; // <---------------------PROBLEM HERE---------------

int main()
{
    int integer=3;
    jar A(integer);

    cout<<*(jar::jarlist.front())->value);

    return 0;
}

 

any help would be greatly appreciated

Edited by xinfinite33

Share this post


Link to post
Share on other sites
Advertisement

Below the class you could add:

 

template<typename T>
std::deque<jar<T>*> jar<T>::jarlist;
Edited by Nyssa

Share this post


Link to post
Share on other sites
Your constructor contains a bug, it stores a pointer to a local variable as a member. Consider passing a (const) reference, or passing the pointer explicitly.

The destroy function appears to be dangerous, as it attempts to delete objects which are not guaranteed to be allocated with new. Your example program contains this bug.

Your class should probably obey the rule of three, or you should implement a destructor and mark the class as non-copyable.

The "jars" static counter is probably unnecessary, as the deque knowns how many objects it contains.

Finally, such a design is usually unnecessary and the static member can be factored elsewhere, removing or exposing the state dependency. Edited by rip-off

Share this post


Link to post
Share on other sites

Yeah I know its buggy, I was mostly experimenting with templates and pointers as a way to make my own memory management system. It failed, miserably. I guess i should research things a bit more before jumping in head first wacko.png . The "rule of three" and the "non-copyable" articles were helpful, as I had no knowledge about those either. But thanks for the comments and suggestions! smile.png

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!