echo "int main(){}" | gcc -Wall -o testbinary -xc++ -
works but I would like to know how it works and better yet, if there is a way to do this without the need to pipe the contents.
Alternatively you can say (e.g. in a shell-script):
gcc -Wall -o testbinary -xc++ - << EOF
int main(){}
EOF