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. Managed C++/CLI
  4. can anybody tell me why the size of an empty class is 1

can anybody tell me why the size of an empty class is 1

Scheduled Pinned Locked Moved Managed C++/CLI
6 Posts 4 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.
  • P Offline
    P Offline
    philiptabraham
    wrote on last edited by
    #1

    can anybody tell me why the size of an empty class is 1.and i also want to know why we cant create an object for an abstract class. Thanks in advance

    G L M 3 Replies Last reply
    0
    • P philiptabraham

      can anybody tell me why the size of an empty class is 1.and i also want to know why we cant create an object for an abstract class. Thanks in advance

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      C++ requires freestanding empty objects to have non-zero size. Most compilers automatically insert a char into "empty" objects to satisfy this. However, if your empty class is a base class then it won't add any size to the derived class object. See: http://msdn2.microsoft.com/en-us/library/f42z47h2(VS.80).aspx[^] You cannot create an object directly from an abstract class since it contains virtual method(s) without implementation(s).

      "We make a living by what we get, we make a life by what we give." --Winston Churchill

      modified on Monday, December 10, 2007 3:25:41 PM

      1 Reply Last reply
      0
      • P philiptabraham

        can anybody tell me why the size of an empty class is 1.and i also want to know why we cant create an object for an abstract class. Thanks in advance

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Giving them the smallest possible non-zero size makes sure different objects result in different pointer/reference values, which is what you hope to get isn't it? :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Sorry for any delays in replying, I currently don't get e-mail notifications.


        1 Reply Last reply
        0
        • P philiptabraham

          can anybody tell me why the size of an empty class is 1.and i also want to know why we cant create an object for an abstract class. Thanks in advance

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          philiptabraham wrote:

          i also want to know why we cant create an object for an abstract class

          If you could, the class wouldn't be abstract. It doesn't make sense to create an object of a class that has virtual functions that are NULL. Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          modified on Monday, December 10, 2007 3:15:35 PM

          G 1 Reply Last reply
          0
          • M Mark Salsbery

            philiptabraham wrote:

            i also want to know why we cant create an object for an abstract class

            If you could, the class wouldn't be abstract. It doesn't make sense to create an object of a class that has virtual functions that are NULL. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            modified on Monday, December 10, 2007 3:15:35 PM

            G Offline
            G Offline
            George L Jackson
            wrote on last edited by
            #5

            Mark Salsbery wrote:

            It doesn't make sense to create an object of a class that doesn't have a complete implementation.

            You can make a class non-copyable by declaring a private copy constructor (and assignment operator) without an implementation!

            "We make a living by what we get, we make a life by what we give." --Winston Churchill

            M 1 Reply Last reply
            0
            • G George L Jackson

              Mark Salsbery wrote:

              It doesn't make sense to create an object of a class that doesn't have a complete implementation.

              You can make a class non-copyable by declaring a private copy constructor (and assignment operator) without an implementation!

              "We make a living by what we get, we make a life by what we give." --Winston Churchill

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              right. badly worded - I changed it a bit. It took a long time for CP to let me edit it (or reply to you :)). Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              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