Hi, this is my second topic here and here I´d like to discuss about C++ coding style. Below I have two files, header and source file I have created some time ago, containing one class. This is supposed to be part of the bigger project I´m working on, called GOOS (Game Object Oriented Script).
For symbols names inside the namespaces, you can then refer to the class by typing only Power,
and GOOS::Entity::Acci::Power from outside the namespace
All the other types and constants starting by GOOS would pass in namespace GOOS and see their name shortened and their scope better defined (IMO)
His name choices are consistent and well-formed. There is no need to go restructuring everything to avoid them.
@HenriK
Your operators are not quite right, particularly ones that must return a value, like the comparison operators. See the Wikipedia page for examples of what the operator should return and the const-ness of non-integer argument types: http://en.wikipedia.org/wiki/Operators_in_c_and_c%2B%2B
That is good suggestion, perhaps I´d better remove extra '_' -characters in front of the header definition thing. I´ll keep that in mind, but Hungarian notation I´d like to leave. That helps me to figure out the type of my variable more easily.
@bartoli
I´ve heard that many programmers use namespaces like you tend to do. However, when my project various different classes, one header file would be pretty big compared to source files including declarations of the classes and that alone may make code shorter but bit more difficult to maintain. Not sure about this, though, perhaps handy, nifty linking might do the trick anyway.
@Duoas
Thanks for the praises. Have to check your link, it may be helpful.
Thanks to all about your feedback. My problem with programming is that I´m not sure whether I´m doing everything right. My coding style may be lacking or my programs may work illogically, but that´s what happens, when you haven´t even turned 17 and haven´t got any advanced teaching (at school we have only talked about variables, operators, functions and arrays and that´s about it).
But Duoas, why these comparison operators have to return a value? All I want to do is change values of the class itself and results work fine even without the return.
If I am comparing two integers, say -7 and 12, I want to know whether the comparison is true or false.
-7 < 12 --> true
-7 > 12 --> false
The "<" and ">" operators are functions that take two integers and return a boolean value.
How does it make sense for me to say 9 <= 4; and discover that the 9 is somehow changed?
If you overload the comparison operators, you should not change the way they work. Hence, I can write:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
struct point
{
int x, y;
point( int x = 0, int y = 0 ): x( x ), y( y ) { }
};
booloperator == ( const point& lhs, const point& rhs )
{
return (lhs.x == rhs.x) && (lhs.y == rhs.y);
}
booloperator != ( const point& lhs, const point& rhs )
{
return !( lhs == rhs );
}
1 2 3 4 5
point a( -7, 12 );
point b( 7, 12 );
if (a == b) cout << "equal (oh no!)\n";
else cout << "not equal (good job!)\n";
The operators tell me something about the relative qualities of my two points, but it did not modify them. In fact, it didn't do anything at all except tell me something useful in the if statement, so that I can choose what to do next.
Perhaps I didn´t explain correctly what I ment. OK, these two comparision operator overloadings have actually totally different functionality than we normally have in C++ codes. They are used as increasement and decreasement of my power, and that´s about it. However, if they would work as they usually would, then I understand (at least for the most part) that it makes a perfect sense.
Conclusion:
Comparision operations accept two values to check and then return boolean value, whether the statement is true or false.
Please correct if I´ve understood things wrong. And thanks a lot!
You can overload operators to behave however you want, just know that it may be confusing to do so for others reading your code. Also, you can overload pre-increment/decrement to be different from post-increment/decrement. ;)
Just because C and C++ allow you to do a thing does not mean that you should.
OK, these two comparision operator overloadings have actually totally different functionality than we normally have in C++ codes.
And that there is the problem with your code. You are doing something the Wrong Way. If you want to do something different than compare two things, then why are you using the "compare things" form?
Make your code clear, not obtuse. You have already got functions to do what you want, anyway, and that are much more clear to understand. Why not just make them public? Or if you must hide the details with your magic constant, use an inline alias:
1 2 3 4 5
class GOOS_Entity_Acci_Power {
...
public:
void PowerUp(ushort amount) {f_CheckChangePowerExtension(GOOS_OPERATOR_GREATERTHANEQUAL,amount);}
};
Using this code is so much easier to read and understand, and costs nothing.
While I'm at it, why are you using typedef like that? (Don't.)
Typedef is used, because I wanted to have one function taking care of basic operations. I´ll try to consider your suggestion, Duoas or perhaps I´ll change <= and/or >= to << and/or >> -operators.
Those are the postfix increment/decrement. Their definition must be operator++(int); in order to difference them from the prefix, they don't receive a parameter. (it's just a syntactic rule)
Curious about what do you want to do with that typedef