Thread: Let's have some fun with obscure C++ features!!!

  1. #1
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Let's have some fun with obscure C++ features!!!

    I'll go first:
    Code:
    #include <iostream>
    
    class Test
         {
         public:
              Test()
                   {
                   std::cout << "Construct" << std::endl;
                   }
              ~Test()
                   {
                   std::cout << "Destruct" << std::endl;
                   }
              void Refresh()
                   {
                   this->~Test();
                   new ((void*)this) Test();
                   }
         };
                         
    int main(void)
         {
    
         Test test;
         test.Refresh();
    
         return 0;
         }
    This code "explicity" calls both the destructor and constructor inside a call to itself.
    doesn't seem like it should work does it?!?!
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    It might work, but itt'l create a memory leak

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    nope! it doesn't. The memory is allocated in main and destroyed at the end of main!
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Okay, this is confusing!
    I'm going to test er out.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    the new operator doesn't allocate memory when you pass the pointer into it. It only calls the constructor that is appropriate. Scared of C++ yet?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >I'm going to test er out.<

    Look up placement new while you're at it.
    Joe

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by JoeSixpack
    >I'm going to test er out.<

    Look up placement new while you're at it.
    I'm well aware of how placement new works. Thanks for the suggestion though
    Edit: Haha, I undestand now. That's clever.
    Last edited by Eibro; 01-31-2003 at 04:21 PM.

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    now now, don't take it personally. very few people ever have a need to use it themselves. (although it's heavily used in STL without their knowledge). And I don't think JoeSixPack meant anything remotely condescending by it.

    any other little comments on this? I have one. I didn't use "placement delete" because it's really not necessary to explicitly call a destructor. I'm not even sure it's standard
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >I'm well aware of how placement new works.<

    Sorry; I assumed that it was this that had confused you into making your first response.
    Joe

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    No no, when I first glanced at that line I though you were casting void* on new, my mistake.
    I'll just shut up now
    edit: It wasn't until I started trying to explain the memory leak that I realized I was wrong
    Last edited by Eibro; 01-31-2003 at 04:29 PM.

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    I like it, *this always has the correct alignment for your object. Now, if you want to see ugly
    Code:
    #include<iostream>
    
    class animal {
    public:
        virtual void speak() = 0;
        virtual void badidea() = 0;
        virtual ~animal() {};
    };
    
    class dog : public animal {
    public:
        dog() { std::cout << "Doggie born" << std::endl; }
        virtual ~dog() { std::cout << "Doggie gone" << std::endl; }
        virtual void speak() { std::cout << "Woof" << std::endl; }
        virtual void badidea();
    };
    
    class cat : public animal {
    public:
        cat() { std::cout << "Kitty born" << std::endl; }
        virtual ~cat() { std::cout << "Kitty gone" << std::endl; }
        virtual void speak() { std::cout << "Meow" << std::endl; }
        virtual void badidea();
    };
    void dog::badidea() { this->~dog(); new ((void*)this) cat(); }
    void cat::badidea() { this->~cat(); new ((void*)this) dog(); }
    
    int main() {	
        animal *p = new dog();
        p->speak();
        p->badidea();
        p->speak();
        delete p;
        return 0;
    }
    Ta da! self mutilating code! I would call a moderator, someone posted an obscenity.

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    good example. gotta make sure you have enough memory for an object when you change it like that!

    how bout this:

    Code:
    class Test
       {
       public:
          Test() { data = 1; }
          ~Test() { data = 2; }
          int data;
       };
    
    int main()
       {
       Test *test = (Test*)new char[sizeof(Test)];
       memset(test, 0, sizeof(Test));
       cout << test.data << endl;
       new (void*)test Test();
       cout << test.data << endl;
       test->~Test();
       cout << test.data << endl;
       delete [] (char*)test;
       }
    output is 0, 1, and 2
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #13
    here is the most complicated C++ program EVER. It can be compiled on C compilers as well with no problem.

    Code:
    int main()
    {
       return 0;
    }

  14. #14
    Registered User SubLogic's Avatar
    Join Date
    Jan 2003
    Posts
    33
    no idea...

    looool
    0100001

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Auto Calling Derived Fun
    By Arlenik in forum C++ Programming
    Replies: 27
    Last Post: 06-02-2008, 11:17 AM
  2. how to add features to a game?
    By mass in forum Game Programming
    Replies: 5
    Last Post: 08-04-2006, 01:10 PM
  3. Programming for fun and profit
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 12-16-2004, 04:52 PM
  4. Fun Learning a New Language
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-30-2003, 10:03 PM