From Nightfox@DIGDIST to All on Friday, November 22, 2013 19:34:54
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
Elija Barnes
Sunday, March 01, 2026 20:14:37
from
Fairborne, Tx
via
HTTPS
Elija Barnes
Saturday, February 28, 2026 19:09:25
from
Fairborne, Tx
via
HTTPS
George H
Friday, February 27, 2026 12:17:56
from
Buffolo, Ny
via
HTTPS
George H
Monday, February 23, 2026 23:12:08
from
Buffolo, Ny
via
HTTPS
Elija Barnes
Monday, February 23, 2026 23:05:47
from
Fairborne, Tx
via
HTTPS
Jim Bob
Monday, February 23, 2026 22:53:59
from
Idaho
via
HTTPS