Jump to content
  • Advertisement
Sign in to follow this  
Dominik2000

Unique Id production

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

Hello,

 

I have found a code snippet in this forum, to generate unique ids for each type.

#ifndef UNQIUETYPEID_HPP
#define UNQIUETYPEID_HPP


unsigned int generateNextId()
{
    unsigned int nextId = 0;
    return ++nextId;
}


template< class T >
class UniqueTypeId
{
public:
    static unsigned int m_Id;
};


template< class T >
unsigned int UniqueTypeId<T>::m_Id = generateNextId();


#endif // UNQIUETYPEID_HPP

The error multiple definitions of generateNextId occurs. But even if I write UniqueNextId::generateNextId an error occurs. Why?

 

Share this post


Link to post
Share on other sites
Advertisement

The reason for that error is because the implementation (last 2 lines of C++ code) is seen in every source that includes this header. It must be in exactly one source (i.e. not in the header).

 

Also, you're lacking a static for unsigned int nextId = 0; inside function generateNextId which will always return and ID of 1 (since it's a local variable) -- so if this compiles, it won't work as expected.

Edited by samoth

Share this post


Link to post
Share on other sites
You also can not have the implementation of generateNextId in the header. Only declare it there and implement it in a cpp-file

Share this post


Link to post
Share on other sites

You also can not have the implementation of generateNextId in the header. Only declare it there and implement it in a cpp-file

 

Either that, or type 

inline unsigned int generateNextId()
{
    unsigned int nextId = 0;
    return ++nextId;
}

Specifying inline storage will make the linker ignore that there are multiple functions that are the same.

Share this post


Link to post
Share on other sites

 

You also can not have the implementation of generateNextId in the header. Only declare it there and implement it in a cpp-file

 

Either that, or type 

inline unsigned int generateNextId()
{
    unsigned int nextId = 0;
    return ++nextId;
}

Specifying inline storage will make the linker ignore that there are multiple functions that are the same.

 

That wont work because it needs to be a single function with a single static variable inside to generate unique id numbers.

Share this post


Link to post
Share on other sites

 

 

You also can not have the implementation of generateNextId in the header. Only declare it there and implement it in a cpp-file

 

Either that, or type 

inline unsigned int generateNextId()
{
    unsigned int nextId = 0;
    return ++nextId;
}

Specifying inline storage will make the linker ignore that there are multiple functions that are the same.

 

That wont work because it needs to be a single function with a single static variable inside to generate unique id numbers.

 

The others already pointed that out above, didn't feel the need to repeat what they said. However, it will work fine to have a static variable in an inlined function.

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!