Sign in to follow this  
Dominik2000

Unique Id production

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

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

 

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this