Yes, certainly. But there are a few things to be aware of.
You should compile from source and give a prefix to your home directory:
./configure --prefix=$HOME
Of course add parameters as needed. make install
will then install in /home/yourdir/bin, /home/yourdir/lib, etc. You may need to run 'ldconfig' manually on your lib directory to create shortened symlinks to your libraries.
In your shell environment, add the path to your personal 'bin' directory first, if it is not already done so. For example, add this to your .bashrc:
PATH=$HOME/bin:$PATH
LD_LIBRARY_PATH=$HOME/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH
This will make sure your own copy of gcc will be found first; LD_LIBRARY_PATH does the same for your own dynamic libraries.
Note that newer versions of gcc may require newer versions of binutils, the assembler and various libraries as well so you may end up with a lot more than just gcc. Also, I think it is recommended to first compile just the C compiler (with --enable-languages=C), then recompile gcc with itself to compile the C++ compiler.
Finally, the above may interfere with existing system C++ libraries; in the past I've had problems with linking C++ libraries compiled by different gcc versions into my programs. C libraries usually do not have this problem.