Sometimes it could be very useful to be able to test private members or methods in your unit tests. But how could this be achieved?

In C/C++ you can do this by using a simple macro:

#define protected public
#define private public
  1. Ozkar (2011-02-17 17:05)

    What is this exactly for?

  2. joe (2011-02-17 17:54)

    It’s a dirty hack to get access to private methods and members of a class. Here is an example:class A {private: void doSomething() {}};A a;a.doSomething();This will cause an error, because doSomething is private and only available from other methods inside the class. But sometimes it is useful to access it in test code, so this evil hack could help.

  3. Ozkar (2011-02-18 00:02)

    oh! yeah i see it now xD thanks for the solid explanation.. hehe the example makes it clear!

  4. Jeremy (2011-06-25 20:01)

    Wow that’s dirty.

    It’s also not *technically* legal C++, though you’ll usually get away with it: http://www.gotw.ca/gotw/076.htm

Add your comment now