代码改变世界

关于c语言的赋值和memcpy的问题

2013-01-18 13:52  Clingingboy  阅读(496)  评论(0编辑  收藏  举报

 

参考链接:

http://bbs.chinaunix.net/thread-1651407-2-1.html

http://www.cnblogs.com/qyaizs/articles/2039101.html

貌似感觉是一样的

代码示例:

        template<typename T, bool PODType>
        class ListStore abstract : public Object
        {
        };
        
        template<typename T>
        class ListStore<T, false> abstract : public Object
        {
        protected:
            static void CopyObjects(T* dest, const T* source, vint count)
            {
                if(dest<source)
                {
                    for(vint i=0;i<count;i++)
                    {
                        dest[i]=source[i];
                    }
                }
                else if(dest>source)
                {
                    for(vint i=count-1;i>=0;i--)
                    {
                        dest[i]=source[i];
                    }
                }
            }

            static void ClearObjects(T* dest, vint count)
            {
                for(vint i=0;i<count;i++)
                {
                    dest[i]=T();
                }
            }
        public:
        };
        
        template<typename T>
        class ListStore<T, true> abstract : public Object
        {
        protected:
            static void CopyObjects(T* dest, const T* source, vint count)
            {
                if(count)
                {
                    memmove(dest, source, sizeof(T)*count);
                }
            }

            static void ClearObjects(T* dest, vint count)
            {
            }
        public:
        };