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. A Bug in the land of construction???

A Bug in the land of construction???

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++helpquestion
3 Posts 3 Posters 1 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.
  • G Offline
    G Offline
    Glenn Inman
    wrote on last edited by
    #1

    /* ** This is an interesting C++ bug, well I think it is a bug */ class CTest1 { public: BYTE* pbData; public: CTest1() { pbData = new BYTE[GetDataSize()]; } virtual int GetDataSize() { return 16; } }; class CTest2 : public CTest1 { CTest2() : CTest1() { } virtual int GetDataSize() { return 32; } }; /* ** when CTest2 is created you would expect a buffer of 32 bytes to be allocated ** but lo and behold, you get 16, try it ** ** in c# they got it right, try the same experiment and you will get 32 allocated in CTest2 */ public class CTest1 { public byte[] abyData = null; public CTest1() { abyData = new byte[DataSize]; } virtual int DataSize { get{return 16;} } } public class CTest2 : CTest1 { override int DataSize { get{return 32;} } }

    P N 2 Replies Last reply
    0
    • G Glenn Inman

      /* ** This is an interesting C++ bug, well I think it is a bug */ class CTest1 { public: BYTE* pbData; public: CTest1() { pbData = new BYTE[GetDataSize()]; } virtual int GetDataSize() { return 16; } }; class CTest2 : public CTest1 { CTest2() : CTest1() { } virtual int GetDataSize() { return 32; } }; /* ** when CTest2 is created you would expect a buffer of 32 bytes to be allocated ** but lo and behold, you get 16, try it ** ** in c# they got it right, try the same experiment and you will get 32 allocated in CTest2 */ public class CTest1 { public byte[] abyData = null; public CTest1() { abyData = new byte[DataSize]; } virtual int DataSize { get{return 16;} } } public class CTest2 : CTest1 { override int DataSize { get{return 32;} } }

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      Vtable initiazation takes place in constructor of base class,its obvious that base class function will get called

      1 Reply Last reply
      0
      • G Glenn Inman

        /* ** This is an interesting C++ bug, well I think it is a bug */ class CTest1 { public: BYTE* pbData; public: CTest1() { pbData = new BYTE[GetDataSize()]; } virtual int GetDataSize() { return 16; } }; class CTest2 : public CTest1 { CTest2() : CTest1() { } virtual int GetDataSize() { return 32; } }; /* ** when CTest2 is created you would expect a buffer of 32 bytes to be allocated ** but lo and behold, you get 16, try it ** ** in c# they got it right, try the same experiment and you will get 32 allocated in CTest2 */ public class CTest1 { public byte[] abyData = null; public CTest1() { abyData = new byte[DataSize]; } virtual int DataSize { get{return 16;} } } public class CTest2 : CTest1 { override int DataSize { get{return 32;} } }

        N Offline
        N Offline
        Nemanja Trifunovic
        wrote on last edited by
        #3

        Simply, C++ has a different order of construction than C# or Java. It was a design decision, not a bug. Take a look at this article[^] for more details.


        My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

        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