cplusplus.co.il

Archive for the ‘tricks’ Category

Quines

Posted on: 16/04/2010

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.

Read the rest of this entry »

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.

Read the rest of this entry »

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;
}

Read the rest of this entry »

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?

Read the rest of this entry »

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?

Read the rest of this entry »

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?

Read the rest of this entry »