What is Proxy Class in C++

A proxy is a class that provides a modified interface to another class. Here is an example – suppose we have an array class that we only want to contain binary digits (1 or 0). Here is a first try: struct array1 { int mArray[10]; int & operator[](int i) { /// what to put here … Read more

Static variables in C++

Excuse me when I answer your questions out-of-order, it makes it easier to understand this way. When static variable is declared in a header file is its scope limited to .h file or across all units. There is no such thing as a “header file scope”. The header file gets included into source files. The … Read more

How should I detect unnecessary #include files in a large C++ project?

While it won’t reveal unneeded include files, Visual studio has a setting /showIncludes (right click on a .cpp file, Properties->C/C++->Advanced) that will output a tree of all included files at compile time. This can help in identifying files that shouldn’t need to be included. You can also take a look at the pimpl idiom to … Read more

Convert an int to ASCII character

Straightforward way: char digits[] = {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ }; char aChar = digits[i]; Safer way: char aChar=”0″ + i; Generic way: itoa(i, …) Handy way: sprintf(myString, “%d”, i) C++ way: (taken from Dave18 answer) std::ostringstream oss; oss << 6; Boss way: Joe, write me an int to char … Read more

Looking for C++ STL-like vector class but using stack storage

You don’t have to write a completely new container class. You can stick with your STL containers, but change the second parameter of for example std::vector to give it your custom allocator which allocates from a stack-buffer. The chromium authors wrote an allocator just for this: https://chromium.googlesource.com/chromium/chromium/+/master/base/stack_container.h It works by allocating a buffer where you … Read more

Interacting with C++ classes from Swift

I’ve worked out a perfectly manageable answer. How clean you’d like this to be is entirely based upon how much work you’re willing to do. First, take your C++ class and create C “wrapper” functions to interface with it. For example, if we have this C++ class: class MBR { std::string filename; public: MBR (std::string … Read more