Thread: Classes: constructors, destructors ???

  1. #16
    Registered User
    Join Date
    Jun 2002
    Posts
    106
    let s say you have class that allocates some memory when a object is created constructor is in the game when an object is created and deconstructor is called when you finished your job with the object (maybe you want to delete the memory that the object allocates)
    C++ Makes you Feel Better

    "Gravity connot be held reponsible for people falling in love"--Albert Einstein

  2. #17
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225
    >>ACTUALLY, I'm going to throw a whole bomb into the mix!

    There is a case where constructors and destructurs would not be called!

    try this:



    code:--------------------------------------------------------------------------------
    MyClass *test;

    test = (MyClass*)new char[sizeof(MyClass)];
    test->SomeMemberFunction();
    delete [] (char *)test;
    --------------------------------------------------------------------------------

    this is 100% legal and the constructors and destructors do not get called! what do you think about that? <<


    This is valid. You may have not noticed it but you already have initialized the private data within the pointer class when you allocated memory for it via the 'new' operator, just like you would do when you allocate memory for a structure.

    Remember, a class is a structure too.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  3. #18
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    das only true if your allocation was supposed to be to zero. Anyway, I don't recall "new" zero initting its memory. I've always done that explicitly with a memset.

  4. #19
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Smile

    >>das only true if your allocation was supposed to be to zero. Anyway, I don't recall "new" zero initting its memory. I've always done that explicitly with a memset.<<


    Yah you're right about that. There would be garbage in it if not set to zero. But that would be a good start since you have already allocated memory for it.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constructors and Destructors
    By lasher in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2006, 11:53 PM
  2. Destructors and Constructors on classes
    By Da-Nuka in forum C++ Programming
    Replies: 14
    Last Post: 06-14-2005, 02:08 PM
  3. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 07:35 AM
  4. Constructors and Destructors
    By GravtyKlz in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2003, 10:44 AM
  5. Replies: 2
    Last Post: 01-15-2002, 06:00 PM