I'm curious to know, what is the most abrasive, caustic, abusive but perfectly legal and correct C++ you've ever seen ship out the door?
This question isn't meant to be a C++ bashing session, but a humorous introspection into the world of C++ we allow ourselves (or others) to get away with.
The most abusive C++ I've seen by far is "Analog Literals" by Eelis.
It basically lets you do this:
unsigned int c = ( o-----o
| !
! !
! !
o-----o ).area;
or
unsigned int c = ( o-------------o
|L \
| L \
| L \
| o-------------o
| ! !
! ! !
o | !
L | !
L | !
L| !
o-------------o ).volume;
See here http://www.xs4all.nl/~weegen/eelis/analogliterals.xhtml
#define private public
if (!this) return;
if (!self) return nil;
. - dreamlax
On a project at work I see people go:
if(false){
//Old code here
}
instead of using:
/*
*/
or just using cvs, like normal people.
while (false) ...
- Kristopher Johnson
<* ... *>
comments (or whatever symbols you like), like in PeopleCode. It doesn't take a genius to write a program that reads a file and outputs a similar file but without these comments. - Eduardo León
;
{}
when writing e.g. an empty loop, but how is this abusive? - Mechanical snail
#include <afxwin.h> //basic MFC include
I have seen stuff like this, tricking around with const :-)
class abc {
public:
long GetVal() const
{
return const_cast<abc*>(this)->GetVal();
}
long GetVal()
{
return member_++;
}
private:
member_;
};
Apparently the author hadn't heard of mutable
.
Calling non-virtual functions that do not modify member variables on null pointers like so:
class CObject
{
public:
void CallMeOnNull()
{
int i = 0;
i++;
}
virtual void DontCallMeOnNull()
{
int i = 0;
i++;
}
void DontCallMeOnNull2()
{
m_iTest++;
}
private:
int m_iTest;
};
int test()
{
CObject* pObject = NULL;
pObject->CallMeOnNull(); //IS OK???!!!
pObject->DontCallMeOnNull();
pObject->DontCallMeOnNull2();
}
Here's some downvote-bait. I do a couple things that reduce errors in editing, but look funny, like
MyFunction(1stArg
, 2ndArg
, 3rdArg
... etc. ...
, lastArg
);
because if I want to add a new argument to the function, I often add it at the end, so it is a 1-line edit, rather than 2 lines.
Another example:
if (false){ }
else if (Test1){
... do stuff ...
}
else if (Test2){
... do stuff ...
}
... more tests ...
else {
... do stuff ...
}
Why? because it makes it easier to insert, delete, or re-order the tests without the first one having different syntax (and thus edited differently) from the rest. This is especially useful in generated code.
I occurred to work on a program where the previous programmer systematically checked that address of non dynamic buffers where not NULL... at first it just look dumb, but when applied to a full program it makes it quite harder to read and maintain.
char buf[10];
if (buf != NULL) {
...
}
That was C, but you can do the same with C++ (hopefully not).
delete this;
It's never nice to see suicidal objects, but sometimes it makes sense. The FAQ entry [1] explains why it sometimes makes sense.
[1] http://www.faqs.org/faqs/C++-faq/part6/I once did templates the C (not C++) way: using #include
s, #define
s, #ifdef
s and recursion at compilation time.
Got another one. Calling a constructor on a class explicitly creates and destroys an object straight away:
class CClass
{
public:
CClass()
{
printf("I am teh constructor");
}
~CClass()
{
printf("I am teh destructor");
}
};
//...Somewhere in main
printf("Before constructor test");
CClass::CClass();
printf("After constructor test");
What you will see is this:
Before constructor test
I am teh constructor
I am teh destructor
After constructor test
SDLSurface *surf = "C:\\images\\picture1.jpeg";
I was helping someone get started with SDL, when I noticed that data tips in the Visual C++ debugger showed an actual file path when the mouse pointer was hovered over an SDLSurface pointer, which lead me to discover this piece of code. When I asked him why, he said he didn't know what to do, so he had to just try something. He's in the 3rd year of a CS master.
#include <stdio.h>
String batman_dialog = "BOOM POW WHACK KAPOW!!!!!"; String robin_dialog = "Holy caps batman"; String bg_music = "na na na na na na na na na na na na na batman!!!"