I gave a brief explanation HERE.
Need more, eventually snippet code, examples, demo application? ;)
Printable View
I gave a brief explanation HERE.
Need more, eventually snippet code, examples, demo application? ;)
That is not because it contains only valid C code that it was written in C (of course, you have seen the true project, so, you know).Quote:
Originally Posted by Skoons
I just quoted your text to introduce the fact I found some "apparently C codes" which revealed, after a further examination, to be C++ (pass by references used somewhere for example).
I have often seen this which can lead to problems:
if(result = DoSomeStuff( data))
{
...
}
And this is common for beginners and is particularly hard to find in order to fix:
if(this = that)
{
do_stuff();
}
Being as 'this' will become 'that' and will always equal that. So this conditional will always be true.
Maybe not a 'beginners' error:
if(object->next->value == value)
{
do_stuff();
}
When there is no reason to assume that the 'next' object actually exists...
:cool:
You can make it clearer by sayingQuote:
Originally Posted by Zorro3D
if ( ( result = DoSomeStuff( data ) ) != 0 )
{
...
}
I have been known not to bother to put the !=0 comparison it. It's usually obvious that you are intending to assign result and continue into the block if it's non-zero.
won't compile. this is not an l-value.Quote:
And this is common for beginners and is particularly hard to find in order to fix:
if(this = that)
{
do_stuff();
}
Being as 'this' will become 'that' and will always equal that. So this conditional will always be true.
and if this is done in C++ it's a beginners error to be using their own list and not std::list and algorithms. In fact it's a beginners error to use linear search in situations where either std::map or a sorted vector would be a better idea.Quote:
Maybe not a 'beginners' error:
if(object->next->value == value)
{
do_stuff();
}
When there is no reason to assume that the 'next' object actually exists...
:cool:
I go crazy when I see something like this:
And I've seen code like that.Code:#define AFUNC1(a, b) AFunc(a, b)
#define AFUNC2(a) AFunc(a, NULL)
void AFunc(int a, const char* b)
{
// don something
}
...
AFUNC2(10);
And I wish I had a gun when I see things like this:
Code:#include <iostream>
using namespace std;
#define BEGIN \
if(index<0 || index>=5) \
return; \
goo temp = _goo[index]; \
temp.Init(); \
#define END \
temp.Close();
class goo
{
public:
goo(){}
goo(const goo& cpy) {}
const goo& operator=(const goo& rval) { return *this;}
void Init() { cout << "goo::init" << endl;}
void Action() {cout << "goo::action" << endl;}
void Reaction() {cout << "goo::reaction" << endl;}
void Close() {cout << "goo::close" << endl;}
};
class foo
{
goo *_goo;
public:
foo();
~foo();
void do_something(int index);
void do_something_else(int index);
};
foo::foo()
{
_goo = new goo[5];
}
foo::~foo()
{
delete [] _goo;
}
void foo::do_something(int index)
{
BEGIN
temp.Action();
END
}
void foo::do_something_else(int index)
{
BEGIN
temp.Reaction();
END
}
int main()
{
foo f;
f.do_something(0);
f.do_something_else(4);
return 0;
}
What do you think? This one...
... could be funny, stupid, or it's really a bad joke?Code:for(int i = 0; i < nCount; i++)
{
try
{
// something...
}
catch(...)
{
i --; // try again
}
}