Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CSimpleArray ?

CSimpleArray ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    ZBUILDER
    wrote on last edited by
    #1

    Excuse me. Today,I study the WTL Library's CSimpleArray class source code,I find the following code:

    template<class T>
    class CSimpleArray
    {
          T* m_aT;
          ...
    
    	class Wrapper
    	{
    	public:
    		Wrapper(T& _t) : t(_t)
    		{
    		}
    		template 
    		void *operator new(size_t, _Ty* p)
    		{
    			return p;
    		}
    		T t;
    	};
    	void SetAtIndex(int nIndex, T& t)
    	{
    
    		new(&m_aT[nIndex]) Wrapper(t);
    	}
       ...
    }
    

    I can not understand the syntax about "new(&m_aT[nIndex]) Wrapper(t)":confused:,anyone can explain it? thanks first. SIMPLE IS BEAUTY

    R J 2 Replies Last reply
    0
    • Z ZBUILDER

      Excuse me. Today,I study the WTL Library's CSimpleArray class source code,I find the following code:

      template<class T>
      class CSimpleArray
      {
            T* m_aT;
            ...
      
      	class Wrapper
      	{
      	public:
      		Wrapper(T& _t) : t(_t)
      		{
      		}
      		template 
      		void *operator new(size_t, _Ty* p)
      		{
      			return p;
      		}
      		T t;
      	};
      	void SetAtIndex(int nIndex, T& t)
      	{
      
      		new(&m_aT[nIndex]) Wrapper(t);
      	}
         ...
      }
      

      I can not understand the syntax about "new(&m_aT[nIndex]) Wrapper(t)":confused:,anyone can explain it? thanks first. SIMPLE IS BEAUTY

      R Offline
      R Offline
      Renjith Ramachandran
      wrote on last edited by
      #2

      ZBUILDER wrote: I can not understand the syntax about ZBUILDER wrote: new(&m_aT[nIndex]) Wrapper(t); returns the pointer and that new allocates using that pointer * * * Code the dreams * * *

      Z J 2 Replies Last reply
      0
      • R Renjith Ramachandran

        ZBUILDER wrote: I can not understand the syntax about ZBUILDER wrote: new(&m_aT[nIndex]) Wrapper(t); returns the pointer and that new allocates using that pointer * * * Code the dreams * * *

        Z Offline
        Z Offline
        ZBUILDER
        wrote on last edited by
        #3

        Then What about Wapper(t)? And How does it modify the Adderss of &m_aT[nIndex]? SIMPLE IS BEAUTY

        1 Reply Last reply
        0
        • R Renjith Ramachandran

          ZBUILDER wrote: I can not understand the syntax about ZBUILDER wrote: new(&m_aT[nIndex]) Wrapper(t); returns the pointer and that new allocates using that pointer * * * Code the dreams * * *

          J Offline
          J Offline
          Jorgen Sigvardsson
          wrote on last edited by
          #4

          It's called placement new. You're executing the Wrapper-constructor on an already allocated memory block. -- Only in a world this shitty could you even try to say these were innocent people and keep a straight face.

          1 Reply Last reply
          0
          • Z ZBUILDER

            Excuse me. Today,I study the WTL Library's CSimpleArray class source code,I find the following code:

            template<class T>
            class CSimpleArray
            {
                  T* m_aT;
                  ...
            
            	class Wrapper
            	{
            	public:
            		Wrapper(T& _t) : t(_t)
            		{
            		}
            		template 
            		void *operator new(size_t, _Ty* p)
            		{
            			return p;
            		}
            		T t;
            	};
            	void SetAtIndex(int nIndex, T& t)
            	{
            
            		new(&m_aT[nIndex]) Wrapper(t);
            	}
               ...
            }
            

            I can not understand the syntax about "new(&m_aT[nIndex]) Wrapper(t)":confused:,anyone can explain it? thanks first. SIMPLE IS BEAUTY

            J Offline
            J Offline
            Jorgen Sigvardsson
            wrote on last edited by
            #5

            ZBUILDER wrote: new(&m_aT[nIndex]) Wrapper(t) It's called placement new. It executed the constructor Wrapper(t) on a preallocated memory block &m_aT[nIndex]. So basically, you're telling new not to allocate new memory for the object but use existing memory. -- Only in a world this shitty could you even try to say these were innocent people and keep a straight face.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups