Obscure, useful C/C++ stuff menu

User Tag List

Results 1 to 1 of 1
  1. #1
    nanidin's Avatar Member
    Reputation
    4
    Join Date
    Jun 2008
    Posts
    9
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Obscure, useful C/C++ stuff

    There are quite a few preprocessor defines that end up being quite useful for developing / debugging apps in C/C++... but they're pretty obscure and not many know about them... so I thought I'd pass on the info


    These guys are useful for creating more meaningful error messages and for locating bugs.
    __LINE__ - Becomes the line number that the code is located on
    __FILE__ - Becomes the file the line is in
    __FUNCTION__ - Becomes the function that the line is in

    With these, you can now avoid putting stuff in debug statements like "made it here!" and then forgetting where that happened in the code! With a macro like this:
    Code:
    #define DEBUG printf("File: %s, Line: %s\n", __FILE__, __LINE__)
    you can plop "DEBUG" anywhere in your code and it will tell print out the line and file that you put that statement on...



    In C++, you can access the runtime typing information via the typeinfo functions. This may not be very useful to you unless you're writing OO code that relies on polymorphism... but again, this is one of those things that not a lot of people know about that can end up being handy.
    Code:
    #include <typeinfo>
    
    class animal { ... };
    class dog : public animal { ... };
    class cat : public animal { ... };
    
    int main()
    {
       animal* animals[2];
       animals[0] = new dog();
       animals[1] = new cat();
    
       //Your code could add more animals, reorder them... then you won't know which type of animal is in each position. It's probably bad design if you rely on typeinfo like I'm about to use it, but this is a good example of why it can be useful :)
    
      int dogCount = 0;
      for(int i=0; i < 2; ++i)
      {
         if(typeid(*animals[i]) == typeid(dog)) { dogCount++; }
         if(typeid(*animals[i]) == typeid(cat)) { dynamic_cast<cat*>(animals[i])->eatAMouse(); } 
      }
    }
    There are probably a lot of ways to accomplish the above - just off of the top of my head, you could try dynamic_casting to different types, and if it returned NULL then you'd know it wasn't that type. You can get the decorated classname if you do typeid(class).name() -- with this, you can accomplish some things that dynamic casting won't let you do -- for instance, you could perform an action if the class name contains a certain word. (I've just noticed I began to leave the scope of the thread. If anyone wants more insights into this, let me know and I'll make another post).

    I hope someone out there finds these things useful :wave:

    Obscure, useful C/C++ stuff

Similar Threads

  1. [Question] Anyone using Vmware for stuff? I can't stop the mouse glitches.
    By Scott McKay in forum WoW Bots Questions & Requests
    Replies: 1
    Last Post: 11-20-2016, 06:41 PM
  2. Just wondering about the practical use of some stuff :-)
    By Jaladhjin in forum WoW Bots Questions & Requests
    Replies: 1
    Last Post: 08-28-2012, 06:33 PM
  3. [SVN] Useful stuff
    By LJN in forum World of Warcraft Emulator Servers
    Replies: 2
    Last Post: 08-20-2008, 09:44 AM
  4. Useful Stuff
    By Asphire in forum World of Warcraft Emulator Servers
    Replies: 4
    Last Post: 12-09-2007, 12:37 PM
  5. Some useful stuff.. for wow that is!
    By Nolixz in forum World of Warcraft Guides
    Replies: 11
    Last Post: 01-15-2007, 07:05 PM
All times are GMT -5. The time now is 08:41 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search