Strange problems caused by inline

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.