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. sizeof(class) question

sizeof(class) question

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
3 Posts 2 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.
  • K Offline
    K Offline
    kfaday
    wrote on last edited by
    #1

    Hi, i've got this class template class pointer { private: T *p; }; and i get 4 if i do sizeof(pointer). if i add a static member data, it doesn;t count. why is it that? is it because there's only one copy of that atribute for all the instances of the class? then, if i add a virtual function (or 2, or 3), i get 8 if i do sizeof(pointer). why is that? thanks for your help!

    G 1 Reply Last reply
    0
    • K kfaday

      Hi, i've got this class template class pointer { private: T *p; }; and i get 4 if i do sizeof(pointer). if i add a static member data, it doesn;t count. why is it that? is it because there's only one copy of that atribute for all the instances of the class? then, if i add a virtual function (or 2, or 3), i get 8 if i do sizeof(pointer). why is that? thanks for your help!

      G Offline
      G Offline
      gokings
      wrote on last edited by
      #2

      Adding a static member does not affect the size because a static member does not exist in an object (instantiation) of the class. The memory for the static member is allocated in ONE place, not for each object. The reason that the size increases when you add a virtual method, is that the object needs a pointer to the VTable. This is a table of pointers, one for each virtual method. This table is specific to your class. Each object of the class will contain a pointer to this VTable. The size of this pointer is 4 bytes.

      K 1 Reply Last reply
      0
      • G gokings

        Adding a static member does not affect the size because a static member does not exist in an object (instantiation) of the class. The memory for the static member is allocated in ONE place, not for each object. The reason that the size increases when you add a virtual method, is that the object needs a pointer to the VTable. This is a table of pointers, one for each virtual method. This table is specific to your class. Each object of the class will contain a pointer to this VTable. The size of this pointer is 4 bytes.

        K Offline
        K Offline
        kfaday
        wrote on last edited by
        #3

        thanks~!

        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