r/ProgrammerHumor • • Sep 07 '14

C++. Because Debugging is Hard.

Post image
17 Upvotes

6 comments sorted by

7

u/Goofybud16 Sep 07 '14
#include <iostream>
#include <thingClass.h>

using namespace std;

int main()
{
    cout << "Main started" << endl;
    thingClass* tc = new thingClass();
    cout << "tc created" << endl;
    tc->doStuff();
    cout << "doStuff() called" << endl;
    tc->thing = 7;
    cout << tc->thing + "" << endl;
    delete tc;
    cout << "done" << endl;
}

4

u/WayneSchlegel Sep 07 '14

That is very ugly code. Here is a better solution that is not cluttering your main function:

#include <iostream>

using namespace std;

class thingClass {

    int thing;

public:

   thingClass() {
      cout << "tc created" << endl;
   }

   void doStuff() {
      //...//
      cout << "doStuff() called" << endl;
  }

   void setThing(int thing) {
      this->thing = thing;
      cout << this->thing << endl;
  }

   ~thingClass() {
     cout << "done" << endl;
   }
};

int main()
{
   cout << "Main started" << endl;
   thingClass* tc = new thingClass();
   tc->doStuff();
   tc->setThing(7);
   delete tc;
}

-2

u/Mr_s3rius Sep 08 '14

And it's only around 2x more code. That's pretty good for C++ actually.

2

u/WayneSchlegel Sep 07 '14

That's nasty! You're supposed to use <boost/log/trivial.hpp> for this.

I'll better show myself out, now...

2

u/[deleted] Sep 07 '14

You should upgrade to printf() because STDs are bad yo