Archive for the ‘tricks’ Category
Quines
Posted on: 16/04/2010
- In: tricks
- 5 Comments
A Quine is a computer program which prints a copy of its own source code as its only output.
Thus it is theoretically possible to compile such a program, run it, and then have its output compiled again to produce the initial program – in an infinite loop, forever.
Escaping overloaded operators
Posted on: 19/02/2010
- In: tricks
- 6 Comments
The possibility of overloading just about any C++ operator and having it do something entirely different from what it was designed for, can sometimes make life pretty hard.
Here are a couple of examples: What if you wanted to take the address of an object, which had implemented an entirely different semantic for the ampersand (&) operator? Or what if somebody decided to overload the comma operator in some strange manner?
As you could have guessed, there are solutions for such scenarios.
The omnipotent arrow operator
Posted on: 28/12/2009
- In: tricks
- 2 Comments
Are you familiar with the new, all-mighty, arrow “–>” operator in C++ ?
#include <iostream>
int main () {
unsigned count = 30;
while (count --> 0) // count goes to zero
std::cout << count << std::endl;
}
Suppose you wanted to check the size (in bytes) of a certain type, WITHOUT using the sizeof() operator. How would you do that?
And what is the size of an empty struct (or class), anyway?
- In: tricks
- 8 Comments
Suppose we wanted to check the sizeof or the offset of a certain member within our struct (or class), without actually having an instantiated object to run the needed operations on. How would you do that?
Changing the vtable pointer
Posted on: 14/08/2009
Most compilers implement dynamic binding by using a vtable whose pointer resides at the beginning of each object’s memory footprint (something along the lines of [vtable-pointer|..members..], if we are not considering virtual inheritance).
Keeping this idea in mind, why don’t we go ahead and attempt to change that vtable pointer?
Recent comments