From Nightfox@DIGDIST to All on Fri Nov 22 19:34:00 2013
Nasty fun things to do in C++ (AKA Things not to do in C++, AKA How to shoot yourself in the foot in C++):
Do you ever need to access private data members/functions in a class? Are you frustrated that you aren't allowed to do that? No problem - Just put this at the top of your source file:
#define private public
By re-defining private as public, you'll be able to access the private members of an object!
Do you think references are safe? Think again! Consider this code: ////////////////////////////////
int *dynamicInt = new int(3);
int& intRef(*dynamicInt);
cout << "intRef (1): " << intRef << endl;
delete dynamicInt;
cout << "intRef (2): " << intRef << endl;
////////////////////////////////
After executing 'delete dynamicInt;', your program is likely to crash when you try to use intRef again!
Do you think 'const' always guarantees constness? Think again! The const_cast operator can be used to remove constness. Consider this function:
void someFunc(const string& aString)
{
string& str(const_cast<string&>(aString));
str = "Hello";
}
Now, the value of aString has been changed by the function!
These are just a few of the many fun things you can get away with in C++ and potentially shoot yourself in the foot!
Nightfox
---
þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
Who's Online
Recent Visitors
Arpacc
Sat Dec 20 09:37:19 2025
from
Boston, Ma
via
Telnet
Infectedbootsector
Fri Dec 19 21:44:44 2025
from
Severna Park, Md
via
Telnet
Chipcunk
Thu Dec 18 08:18:11 2025
from
Siloam Springs, Ar.
via
HTTP
John F Kennedy
Wed Dec 17 03:19:17 2025
from
Crazyworldbbs.Com:2323
via
Telnet