Strange problems caused by inline
First look at these macros
# Define objectcache_decl_mt/
PRIVATE :/
Mp_thread_type m_nthreadid ;/
Static mtmemorypool <memorypool, primitivelock> _ objectcache ;/
Public :/
Void * operator new (size_t size, mp_thread_type threadid = 0 );/
Void operator Delete (void * doomed );
# Define objectcache_def_size_mt (class, description, sizex )/
Mtmemorypool <memorypool, primitivelock> class: _ objectcache (sizeof (class ));/
Inline void * Class: Operator new (size_t size, mp_thread_type threadid )/
{/
Class * P = (class *) _ objectcache. alloc (threadid );/
P-> m_nthreadid = threadid ;/
Return (void *) P ;/
}/
Inline void class: Operator Delete (void * doomed )/
{/
Return _ objectcache. Free (doomed, (class *) doomed)-> m_nthreadid );/
}
How to use these macros:
In the objecta. h file
Class objecta
{
Objectcache_decl_mt
};
In the objecta. cpp File
Objectcache_def_size_mt (objecta, "memory_pool", 1024 );
Compile these files into a libobjecta.
The problem arises,
In another. cpp file:
# Include "objecta. H"
Then new (0) objecta, compile: gcc xxxxx-lobjecta will report objecta operator new unreferenced .....
Why?
Because New is included in LIBA by inline, but this is not specified in the header file! Therefore, the compiler tries to find the symbol operator new (size, XXX) in libobjecta ....
At first, the problem was hidden deeper, because all files are compiled in the shared lib mode, unreferenced symbol is not checked, and it is only found at runtime that the symbol is invalid!
Conclusion: Do not put inline content in the CPP file!